KnpLabs/KnpTimeBundle is a really simple Bundle which converts date time strings to human friendly "ago" dates with support to many languages out of the box. Here is how you can use it in your Symfony projects.

First things first. Before you use it, make sure you have enabled translation by un-commenting the following line in the app/config/config.yml file.

translator:   { fallbacks: ["%locale%"] }

In order to use it, you should first install the Bundle using composer.

composer require knplabs/knp-time-bundle

Then you need to register this bundle in app/AppKernal.php file.

$bundles = array(
    // ...
    new Knp\Bundle\TimeBundle\KnpTimeBundle(),

And that's it! Now you can use this library in PHP or Twig templates. If you are using PHP views you can call the helper function inside the view.

echo $view['time']->diff($dateTime);

Or if you are using Twig templating use any of the following methods.

{{ dateTime|ago }}
{{ date_diff(dateTime) }}

blog comments powered by Disqus