1``date``
2========
3
4.. versionadded:: 1.1
5    The timezone support has been added in Twig 1.1.
6
7.. versionadded:: 1.5
8    The default date format support has been added in Twig 1.5.
9
10.. versionadded:: 1.6.1
11    The default timezone support has been added in Twig 1.6.1.
12
13.. versionadded:: 1.11.0
14    The introduction of the false value for the timezone was introduced in Twig 1.11.0
15
16The ``date`` filter formats a date to a given format:
17
18.. code-block:: jinja
19
20    {{ post.published_at|date("m/d/Y") }}
21
22The format specifier is the same as supported by `date`_,
23except when the filtered data is of type `DateInterval`_, when the format must conform to
24`DateInterval::format`_ instead.
25
26The ``date`` filter accepts strings (it must be in a format supported by the
27`strtotime`_ function), `DateTime`_ instances, or `DateInterval`_ instances. For
28instance, to display the current date, filter the word "now":
29
30.. code-block:: jinja
31
32    {{ "now"|date("m/d/Y") }}
33
34To escape words and characters in the date format use ``\\`` in front of each
35character:
36
37.. code-block:: jinja
38
39    {{ post.published_at|date("F jS \\a\\t g:ia") }}
40
41If the value passed to the ``date`` filter is ``null``, it will return the
42current date by default. If an empty string is desired instead of the current
43date, use a ternary operator:
44
45.. code-block:: jinja
46
47    {{ post.published_at is empty ? "" : post.published_at|date("m/d/Y") }}
48
49If no format is provided, Twig will use the default one: ``F j, Y H:i``. This
50default can be easily changed by calling the ``setDateFormat()`` method on the
51``core`` extension instance. The first argument is the default format for
52dates and the second one is the default format for date intervals:
53
54.. code-block:: php
55
56    $twig = new \Twig\Environment($loader);
57    $twig->getExtension('\Twig\Extension\CoreExtension')->setDateFormat('d/m/Y', '%d days');
58
59    // before Twig 1.26
60    $twig->getExtension('core')->setDateFormat('d/m/Y', '%d days');
61
62Timezone
63--------
64
65By default, the date is displayed by applying the default timezone (the one
66specified in php.ini or declared in Twig -- see below), but you can override
67it by explicitly specifying a timezone:
68
69.. code-block:: jinja
70
71    {{ post.published_at|date("m/d/Y", "Europe/Paris") }}
72
73If the date is already a DateTime object, and if you want to keep its current
74timezone, pass ``false`` as the timezone value:
75
76.. code-block:: jinja
77
78    {{ post.published_at|date("m/d/Y", false) }}
79
80The default timezone can also be set globally by calling ``setTimezone()``:
81
82.. code-block:: php
83
84    $twig = new \Twig\Environment($loader);
85    $twig->getExtension('\Twig\Extension\CoreExtension')->setTimezone('Europe/Paris');
86
87    // before Twig 1.26
88    $twig->getExtension('core')->setTimezone('Europe/Paris');
89
90Arguments
91---------
92
93* ``format``:   The date format
94* ``timezone``: The date timezone
95
96.. _`strtotime`:            https://secure.php.net/strtotime
97.. _`DateTime`:             https://secure.php.net/DateTime
98.. _`DateInterval`:         https://secure.php.net/DateInterval
99.. _`date`:                 https://secure.php.net/date
100.. _`DateInterval::format`: https://secure.php.net/DateInterval.format
101