Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare date with a specific one and DateTime to string in Twig

Tags:

php

twig

  1. How can I compare two dates in Twig when the first one comes from the dababase and the second is clear - 2012-12-31? I tried with

    {% if  dom.dueDate|date('Y-m-d') > 2012-12-31 %}
    

    but I don't get the result I want. :(

  2. I have a DateTime field but I couldn't find a filter for DateTime in Twig and when I use |date('Y-m-d') it prints only the date without the hour :(

I would be really happy and grateful if someone helps me to solve the problems!

like image 743
Faery Avatar asked Aug 02 '12 09:08

Faery


2 Answers

Try timestamps for comparison :

{% if dom.dueDate|date('U') > '2012-12-31'|date('U') %}

and this to add hours, minutes and seconds

{{ dom.dueDate|date('Y-m-d H:i:s') }}
like image 55
ivoba Avatar answered Sep 24 '22 14:09

ivoba


As of twig 1.6+, the correct way of comparing dates according to the official docs is using the date function:

{% if dom.dueDate > date('2012-12-31') %}
like image 45
giosh94mhz Avatar answered Sep 24 '22 14:09

giosh94mhz