date.rst 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. ``date``
  2. ========
  3. .. versionadded:: 1.6
  4. The date function has been added in Twig 1.6.
  5. .. versionadded:: 1.6.1
  6. The default timezone support has been added in Twig 1.6.1.
  7. Converts an argument to a date to allow date comparison:
  8. .. code-block:: jinja
  9. {% if date(user.created_at) < date('+2days') %}
  10. {# do something #}
  11. {% endif %}
  12. The argument must be in a format supported by the `date`_ function.
  13. You can pass a timezone as the second argument:
  14. .. code-block:: jinja
  15. {% if date(user.created_at) < date('+2days', 'Europe/Paris') %}
  16. {# do something #}
  17. {% endif %}
  18. If no argument is passed, the function returns the current date:
  19. .. code-block:: jinja
  20. {% if date(user.created_at) < date() %}
  21. {# always! #}
  22. {% endif %}
  23. .. note::
  24. You can set the default timezone globally by calling ``setTimezone()`` on
  25. the ``core`` extension instance:
  26. .. code-block:: php
  27. $twig = new Twig_Environment($loader);
  28. $twig->getExtension('core')->setTimezone('Europe/Paris');
  29. .. _`date`: http://www.php.net/date