How to get days difference in twig

耗尽温柔 提交于 2019-12-04 03:21:29
Kay

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');
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!