1The Date Extension 2=================== 3 4The *Date* extension provides the ``time_diff`` filter. 5 6You need to register this extension before using the ``time_diff`` filter:: 7 8 $twig->addExtension(new Twig_Extensions_Extension_Date()); 9 10``time_diff`` 11------------- 12 13Use the ``time_diff`` filter to render the difference between a date and now. 14 15.. code-block:: jinja 16 17 {{ post.published_at|time_diff }} 18 19The example above will output a string like ``4 seconds ago`` or ``in 1 month``, 20depending on the filtered date. 21 22.. note:: 23 24 Internally, Twig uses the PHP ``DateTime::diff()`` method for calculating the 25 difference between dates, this means that PHP 5.3+ is required. 26 27Arguments 28~~~~~~~~~ 29 30* ``date``: The date for calculate the difference from now. Can be a string 31 or a DateTime instance. 32 33* ``now``: The date that should be used as now. Can be a string or 34 a DateTime instance. Do not set this argument to use current date. 35 36Translation 37~~~~~~~~~~~ 38 39To get a translatable output, give a ``Symfony\Component\Translation\TranslatorInterface`` 40as constructor argument. The returned string is formatted as ``diff.ago.XXX`` 41or ``diff.in.XXX`` where ``XXX`` can be any valid unit: second, minute, hour, day, month, year. 42