我想比较两个日期以获得 TWIG 中的时间(天、小时、分钟和秒)差异
{% set todayDate = ( "now"| date("Y-m-d H:i:s") ) %} //2013-04-17 08:45:28
{% set endDate = (enddate|date('Y-m-d H:i:s')) %} //2013-04-18 23:59:59
如何获得小时差?
最后我明白了.. 比较两个日期以获得控制器中的时间(天、小时和分钟):
(教义查询)
TIMESTAMPDIFF(DAY,CURRENT_TIMESTAMP(), a.enddate) AS endday,
(TIMESTAMPDIFF(HOUR,CURRENT_TIMESTAMP(), a.enddate) - TIMESTAMPDIFF(DAY,CURRENT_TIMESTAMP(),a.enddate)*24) AS endhour,
(TIMESTAMPDIFF(MINUTE,CURRENT_TIMESTAMP(), a.enddate) - TIMESTAMPDIFF(HOUR,CURRENT_TIMESTAMP(), a.enddate)*60) AS endmin,
为 TIMESTAMPDIFF 创建了一个函数 https://github.com/beberlei/DoctrineExtensions/blob/master/lib/DoctrineExtensions/Query/Mysql/TimestampDiff.php
并包含一个片段
app->config->config.yml 学说:dbal: ........
orm:
entity_managers:
default:
auto_mapping: true
dql:
datetime_functions:
timestampdiff: Acme\BUNDLE\FOLDER\TimestampDiff
谢谢大家 ..
由于您将结束日期传递给模板,因此您应该在控制器中执行此操作并将结果传递给模板。
这是您可以使用的 php 代码:
$now = new DateTime('now');
$dateToCompare = new DateTime('your date');
$difference = $now->format('U') - $dateToCompare->format('U');
// or if your date to compare is in the future :
// $difference = $dateToCompare->format('U') - $now->format('U');
$time_diff = gmdate('H',$difference);
echo $time_diff;