You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

date.rst 1.2KB

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