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.
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.