blob: 3329a56988ef52386410951349224e4da5b11f43 [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001``date``
2========
3
4Converts an argument to a date to allow date comparison:
5
6.. code-block:: html+twig
7
8 {% if date(user.created_at) < date('-2days') %}
9 {# do something #}
10 {% endif %}
11
12The argument must be in one of PHPs supported `date and time formats`_.
13
14You can pass a timezone as the second argument:
15
16.. code-block:: html+twig
17
18 {% if date(user.created_at) < date('-2days', 'Europe/Paris') %}
19 {# do something #}
20 {% endif %}
21
22If no argument is passed, the function returns the current date:
23
24.. code-block:: html+twig
25
26 {% if date(user.created_at) < date() %}
27 {# always! #}
28 {% endif %}
29
30.. note::
31
32 You can set the default timezone globally by calling ``setTimezone()`` on
33 the ``core`` extension instance::
34
35 $twig = new \Twig\Environment($loader);
36 $twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone('Europe/Paris');
37
38Arguments
39---------
40
41* ``date``: The date
42* ``timezone``: The timezone
43
44.. _`date and time formats`: https://secure.php.net/manual/en/datetime.formats.php