I want to compare two dates. I want to make a condition which checks if the date in my database is older than the date in two days.
Here are my two variables:
{<f:format.date format="d.m.Y">+2 days</f:format.date>}
{<f:format.date format="d.m.Y">{day.valid}</f:format.date>}
I want to solve this in the template, not in the model or the controller.
Assign your date to variable »yourdate«.
<f:if condition="{f:format.date(date: '+2 days', format: 'Y-m-d')} < {f:format.date(date: yourdate, format: 'Y-m-d')}">
<f:then>
yourdate is smaller than now + 2 days.
</f:then>
<f:else>
yourdate is greater than or equal to now + 2 days.
</f:else>
</f:if>
Here is my current solution which adds in a current date and does some calculations with the date from the content.
In the controller, add the current date to the data:
$this->view->assign('date_now', new \DateTime());
This is available as {date_now} in fluid then:
<f:if condition="{f:format.date(date: date_now, format: '(Y-m-d)')} > {f:format.date(date: '{event.date}-4 weeks', format: '(Y-m-d)')}">
<f:then>
<p>Event date is past</p>
</f:then>
<f:else>
<p>Event date is upcoming</p>
</f:else>
</f:if>
Note how on the right side, where some calculation is done, additional quotes and curly brackets come in ('{event.date}-4 weeks'
).
PS I prefer the Y-m-d
format to U
for a date comparison, as we don't want to compare the current time – just the date.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With