5 The timezone support has been added in Twig 1.1.
8 The default date format support has been added in Twig 1.5.
10 .. versionadded:: 1.6.1
11 The default timezone support has been added in Twig 1.6.1.
13 .. versionadded:: 1.11.0
14 The introduction of the false value for the timezone was introduced in Twig 1.11.0
16 The ``date`` filter formats a date to a given format:
20 {{ post.published_at|date("m/d/Y") }}
22 The format specifier is the same as supported by `date`_,
23 except when the filtered data is of type `DateInterval`_, when the format must conform to
24 `DateInterval::format`_ instead.
26 The ``date`` filter accepts strings (it must be in a format supported by the
27 `strtotime`_ function), `DateTime`_ instances, or `DateInterval`_ instances. For
28 instance, to display the current date, filter the word "now":
32 {{ "now"|date("m/d/Y") }}
34 To escape words and characters in the date format use ``\\`` in front of each
39 {{ post.published_at|date("F jS \\a\\t g:ia") }}
41 If the value passed to the ``date`` filter is ``null``, it will return the
42 current date by default. If an empty string is desired instead of the current
43 date, use a ternary operator:
47 {{ post.published_at is empty ? "" : post.published_at|date("m/d/Y") }}
49 If no format is provided, Twig will use the default one: ``F j, Y H:i``. This
50 default can be easily changed by calling the ``setDateFormat()`` method on the
51 ``core`` extension instance. The first argument is the default format for
52 dates and the second one is the default format for date intervals:
56 $twig = new Twig_Environment($loader);
57 $twig->getExtension('Twig_Extension_Core')->setDateFormat('d/m/Y', '%d days');
60 $twig->getExtension('core')->setDateFormat('d/m/Y', '%d days');
65 By default, the date is displayed by applying the default timezone (the one
66 specified in php.ini or declared in Twig -- see below), but you can override
67 it by explicitly specifying a timezone:
71 {{ post.published_at|date("m/d/Y", "Europe/Paris") }}
73 If the date is already a DateTime object, and if you want to keep its current
74 timezone, pass ``false`` as the timezone value:
78 {{ post.published_at|date("m/d/Y", false) }}
80 The default timezone can also be set globally by calling ``setTimezone()``:
84 $twig = new Twig_Environment($loader);
85 $twig->getExtension('Twig_Extension_Core')->setTimezone('Europe/Paris');
88 $twig->getExtension('core')->setTimezone('Europe/Paris');
93 * ``format``: The date format
94 * ``timezone``: The date timezone
96 .. _`strtotime`: http://www.php.net/strtotime
97 .. _`DateTime`: http://www.php.net/DateTime
98 .. _`DateInterval`: http://www.php.net/DateInterval
99 .. _`date`: http://www.php.net/date
100 .. _`DateInterval::format`: http://www.php.net/DateInterval.format