zoom23
zoom23

Reputation: 714

Compare DateTime in Fluid

I want to compare a dateTime Object with the today date.

This is my date Object

<f:format.date format="d.m.Y - H:i:s">{article.validFrom}</f:format.date>

I want to make a condition, if the date Object is bigger than the current day. I will do something.

Can anybody help me ?

Upvotes: 2

Views: 4782

Answers (1)

Merec
Merec

Reputation: 2761

Convert the date to an unix timestamp using format="U" and compare them. You need to add a variable which contains the current date.

In your controller add

$this->view->assign('date_now', new DateTime());
// or for TYPO3 >= 6.2
$this->view->assign('date_now', new \DateTime());

then in your template use following

<f:if condition="{f:format.date(date: article.validFrom, format: 'U')} > {f:format.date(date: date_now, format: 'U')}">
    Date is valid
</f:if>

EDIT: The other way (and I think is the better way) is to add a new method to your model which checks this. Example:

Classes/Domain/Model/Article.php

class Article extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {

    /**
      * @return boolean
      */
    public function getDateIsValid() {
        return ($this->getValidFrom() > new DateTime());
    }

}

then you can simply use

<f:if condition="{article.dateIsValid}"></f:if>

Upvotes: 2

Related Questions