October CMS resources and help articles

Simple and to the point. Optimized by the community.

Compare dates in Twig

7
by OFFLINE, last modified on August 12th, 2019

In Twig you can use "now" | date to get the current date. Format "now" and your date as unix-timestamp. You can then rely on number comparision to compare dates this way:

{% if record.end_date|date('U') > 'now'|date('U') %}

A nicer solution to this problem is to add a getIsPastAttribute method to your model and do the comparision there. This way you can use record.is_past in twig.


class Record extends Model
{
    public function getIsPastAttribute()
    {
        return $this->end_date->lt(now());
    }
}

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?