get a time difference between two dates

前端 未结 2 2061
夕颜
夕颜 2021-01-17 08:15

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\") )          


        
2条回答
  •  没有蜡笔的小新
    2021-01-17 08:48

    Since you pass the enddate to your template, you should do this in your controller and pass the result to your template.

    Here is the php code you could use:

    $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;
    

提交回复
热议问题