11

I'm new to Twig and I'm looking to turn a datetime format into a time ago like 2 hours ago or 3 days ago. There is a jquery plugin (jquery-timeago) that I've been using on the client side but it would be great if I can do this with twig. If twig doesn't come with this filter format are there extensions that I can use?

3 Answers 3

22

Twig's date extension does exactly what you ask:

{{ post.published_at|time_diff }}

The example above will output a string like 4 seconds ago or in 1 month, depending on the filtered date.

See http://twig.sensiolabs.org/doc/extensions/date.html (no longer works) Working link http://twig-extensions.readthedocs.io/en/latest/date.html

To make this work, follow these steps:

composer require twig/extensions

When working with symfony add this to your services.yml

services:
  twig.extension.date:
    class: Twig_Extensions_Extension_Date
    tags:
    - { name: twig.extension }
3
  • 2
    Unfortunately that filter gives undefined (or null?) if the difference is 0 seconds. So, you have to use {{ post.published_at|time_diff ? : 'just now' }}. I recommend writing a custom filter as suggested above.
    – twharmon
    Commented Sep 22, 2016 at 19:53
  • That link does not work. Has this feature been removed from twig? Is there a way to do it now?
    – Bryce
    Commented Jun 9, 2017 at 16:54
  • 1
    in twig/extensions github page it says "This repository is abandoned in favor of Twig Core Extra extensions.". Anyways I tried to install and run time_diff filter. But it returned diff.xxx.xxx string, I could not solve, and then tried KnpTimeBundle. KnpTimeBundle worked fine. Commented Apr 29, 2020 at 17:40
15

I found out I can create a custom filter with twig Twig_SimpleFilter.

$filter = new Twig_SimpleFilter('timeago', function ($datetime) {

  $time = time() - strtotime($datetime); 

  $units = array (
    31536000 => 'year',
    2592000 => 'month',
    604800 => 'week',
    86400 => 'day',
    3600 => 'hour',
    60 => 'minute',
    1 => 'second'
  );

  foreach ($units as $unit => $val) {
    if ($time < $unit) continue;
    $numberOfUnits = floor($time / $unit);
    return ($val == 'second')? 'a few seconds ago' : 
           (($numberOfUnits>1) ? $numberOfUnits : 'a')
           .' '.$val.(($numberOfUnits>1) ? 's' : '').' ago';
  }

});

Then I add it to my Twig environment:

$twig = $app->view()->getEnvironment();//because I'm using Twig in Slim framework
$twig->addFilter($filter);

Use it my template like this:

{{2014-10-11 12:54:37|timeago}}
1
  • I know it's a while since you posted it, but is there a way to add translations to your solution? I was able to inject a Translator object in the service, but Symfony doesn't recognize the "translation-ids".
    – wawa
    Commented Jun 29, 2017 at 15:33
7

If you're using Twig inside of Symfony, check out KnpTimeBundle. Includes "ago" support in multiple languages.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.