Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluid compare dates

Tags:

typo3

fluid

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.

like image 614
zoom23 Avatar asked Apr 10 '13 14:04

zoom23


2 Answers

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>
like image 138
shredding Avatar answered Sep 28 '22 23:09

shredding


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.

like image 37
Urs Avatar answered Sep 29 '22 00:09

Urs