October CMS resources and help articles

Simple and to the point. Optimized by the community.

Modify a date inside twig

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

When iterating over a DateTime object in PHP you have to make sure that you clone the date before you modify it since the modification does not return a new instance.

$date = new DateTime();
$date->add(new DateInterval('P1D'));

$newDate = clone $date;
$newDate->add(new DateInterval('P1D'));

In twig you cannot use the clone keyword but you can use the date_modify twig filter.

{% set date = post.published_at %}
{% set newDate = date | date_modify("+1 day") }

Discussion

0 comments

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