我想比较两个日期以获得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
如何获得小时差异?
答案 0 :(得分:1)
最后我明白了.. 比较两个日期以获得时间(天,小时和分钟) 在控制器中:
(学说查询)
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
感谢所有..
答案 1 :(得分:0)
由于您将enddate传递给模板,因此您应该在控制器中执行此操作并将结果传递给模板。
以下是您可以使用的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;