How to get days difference in twig
Asked Answered
P

3

10
  • how to get days diffrence from current day.

  • tweetedAt: "2015-02-22 09:56:42".

twig

{% for key,value in data.about %}
{% set tweets_date=(value.tweetedAt|date).date("now").format('%a') %}
{% endfor %}

I have also tried

{% set dd='now'|date('d-m-Y') %}
{% set tweets_date=(value.tweetedAt|date).dd.format('%a') %}

Finally I tried but its giving error:

A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{"

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

Error:

<span class="small light_grey">{{tweets_date}}</span>

Impossible to invoke a method ("date") on a string variable ("June 6, 2015 01:06") in AcmeBundle
  • how to get diffrence from tweet_date in days ago form.

Updated my project installing twig/extensions via composer

user@intermsh-OptiPlex-380:~$ composer require twig/extensions
Warning: This development build of composer is over 30 days old. It is recommended to update it by running "/usr/local/bin/composer self-update" to get the latest version.
Using version ~1.2 for twig/extensions
./composer.json has been updated
   Loading composer repositories with package information
   Updating dependencies (including require-dev)
   - Installing twig/twig (v1.18.2)
   Downloading: 100%         

   - Installing twig/extensions (v1.2.0)
   Downloading: 100%         
   Downloading: 100%         

twig/extensions suggests installing symfony/translation (Allow the time_diff output to be translated)
Writing lock file
Generating autoload files
Prefrontal answered 15/6, 2015 at 7:32 Comment(5)
Why not calculate it in the back-end? It would allow you much more flexibility and you can easily change it later without touching the template.Lanai
i think symfony cannot solved date diffrencePrefrontal
Well, you don't really need Symfony2's custom functionality here - you can do this with a pure PHP. Take a look at this solution #1417197Lanai
can you help in finding days diffrencePrefrontal
i have tweetedAt: "2015-02-22 09:56:42". datePrefrontal
M
15

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 %}
Meridithmeriel answered 3/9, 2015 at 1:54 Comment(0)
T
9

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' }
Tashinatashkent answered 15/6, 2015 at 7:38 Comment(10)
The filter "time_diff" does not exist in bundle errorPrefrontal
i have reffer docs of symfony : twig.sensiolabs.org/doc/extensions/date.htmlPrefrontal
when i use $twig->addExtension(new Twig_Extensions_Extension_Date()); in controller it giving 503 errorPrefrontal
I updated my answer to show you how to register this extension in container.Tashinatashkent
still 503 error i done like : acme.twig.acme_extension.date: class: Twig_Extensions_Extension_Date tags: - { name: 'twig.extension' }Prefrontal
i have use in service.yml services: acme.twig.acme_extension: class: Acme\biztradeshowsBundle\Twig\AcmeExtension tags: - { name: twig.extension }Prefrontal
You need install it via composer. I again updated my answer. You can also implement your own filter and register it in container.Tashinatashkent
still 503 i have installer composer then alsoPrefrontal
Your error occurs from another place. I just tested my answer and it works. As I said before: if you can't configure twig-extensions you can create your own extension and use it.Tashinatashkent
Esque c'est possible de translater 'ago' ?Bistro
R
3

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');
}
Roup answered 2/9, 2016 at 0:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.