How to get days difference in twig How to get days difference in twig symfony symfony

How to get days difference in twig


I tried this code and it works.

{% set difference = date(value.tweetedAt|date('Y/m/d')).diff(date('now'|date('Y/m/d'))) %}{% set leftDays = difference.days %}


You need use filters not methods on your objects. And you need to use time_diff filter:

{%set tweets_date=value.tweetedAt|time_diff %}

To make it work you need first install twig-extensions via composer:

composer require twig/extensions

and then include it in service container with appropriate tag:

services:    app.twig.extension.date:        class:        Twig_Extensions_Extension_Date        tags:             - { name: 'twig.extension' }


If the date comes from an entity, you can just:

{{ entity.days }}

And in your entity just implement:

public function getDays(){    return $this->date->diff(new DateTime)->format('%a');}