11

I'm using twig to flag a row in a table, if the date associated with that row is less than 30 days old.

Twig's documentation states that to compare two date objects, you have to convert the object to date first, and then make a comparison this way:

{% if date(yourDate) < date(-'30days') %} ... {% endif %}

However, it doesn't specify how to pass the date format for the left side of the comparison, I understand that Twig's date function is some sort of a wrapper for PHP's date.

In PHP I would usually call:

$myDate = \DateTime::createFromFormat("m/d/Y", $myDate);

but in Twig apparently there's no way to specify the original date's format in order to convert it to another format, or at least it's not in the documentation.

This is what I've tried:

{% if date(d.LastDate) > date('-30days') %}...{% endif %}

{% if d.LastDate | format("Y-m-d") > date('-30days') %}...{% endif %}

{% if date("m/d/Y", d.LastEmailSentDate) > date('-30days') %}...{% endif %}

Those conditions and their variations return the following exception in Symfony2:

 An exception has been thrown during the rendering of a template 
("DateTimeZone::__construct(): Unknown or bad timezone (---)")

My controller is returning a date in the format: m/d/Y and I just want to flag that row if that date is less than 30 days old.

2
  • 1
    You can add a function called getIsDateLessThan31DaysOld() in your entity, and make it return true of false. Then call this function in Twig: {% if d.isDateLessThan31DaysOld %}...{% endif %}. It should be easier to write PHP code than Twig code.
    – A.L
    Nov 4, 2013 at 15:33
  • 3
    This should not belong to templates. Provide your model[s] with a proper method.
    – moonwave99
    Nov 4, 2013 at 16:41

3 Answers 3

28

Compare the two dates by getting the number of seconds since the Unix Epoch (PHP Date Format U)

{% if d.LastDate|date("U") > "-30 days"|date("U") %}
    <p>Less than 30 days old</p>
{% endif %}
18

Twig 1.6 supports date comparison.

{% if date(d.LastDate) > date("-30 days") %}
    <p>Less than 30 days old</p>
{% endif %}

{% if date(d.LastDate) > date("now") %}
    <p>Future date</p>
{% endif %}

http://twig.sensiolabs.org/doc/functions/date.html

3

Since PHP 5.3 There is a way with more accuracy.

{# endDate and startDate are strings or DateTime objects #}
{% set difference = date(endDate).diff(date(startDate)) %}
{% set leftDays = difference.days %}
{% if leftDays > 30 %}
  Less than 30 days old
{% else %}
  More than 30 days old
{% endif %}

Explanation:

PHP 5.3 DateTime object has diff() method which return a DateInterval object with the result difference between endDate and beginDate Twig

Twig date function always return a DateTime object so we can call diff method

Finally we can access to the properties of the DateInterval object or format it with the Twig date filter.

Note: There is no need of wrap endDate or startDate with the date function if the variable is already a DateTime object.

Note2: DateTime is used here as synonym of DateTimeInterface.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.