沃梦达 / 编程问答 / php问题 / 正文

获取两个日期之间的时差

get a time difference between two dates(获取两个日期之间的时差)

本文介绍了获取两个日期之间的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个日期以获取 TWIG 中的时间(天、小时、分钟和秒)差异

I would like compare the two dates to get a time (Days, Hours, minutes and seconds) difference in 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

如何获得时差?

推荐答案

终于搞定了..比较两个日期以获得时间(天、小时和分钟)在控制器中:

Finally i got it.. compare the two dates to get a time (Days, Hours and minutes) in controller :

(教义查询)

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

Created a function for TIMESTAMPDIFF https://github.com/beberlei/DoctrineExtensions/blob/master/lib/DoctrineExtensions/Query/Mysql/TimestampDiff.php

并包含一个片段

app->config->config.yml教义:分贝:........

app->config->config.yml doctrine: dbal: ........

orm:
    entity_managers:
        default:
            auto_mapping: true
            dql:
                datetime_functions:
                    timestampdiff: AcmeBUNDLEFOLDERTimestampDiff

谢谢大家..

这篇关于获取两个日期之间的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:获取两个日期之间的时差

基础教程推荐