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 ``date`` filter accepts strings (it must be in a format supported by the
23 `strtotime`_ function), `DateTime`_ instances, or `DateInterval`_ instances. For
24 instance, to display the current date, filter the word "now":
28 {{ "now"|date("m/d/Y") }}
30 To escape words and characters in the date format use ``\\`` in front of each
35 {{ post.published_at|date("F jS \\a\\t g:ia") }}
37 If the value passed to the ``date`` filter is ``null``, it will return the
38 current date by default. If an empty string is desired instead of the current
39 date, use a ternary operator:
43 {{ post.published_at is empty ? "" : post.published_at|date("m/d/Y") }}
45 If no format is provided, Twig will use the default one: ``F j, Y H:i``. This
46 default can be easily changed by calling the ``setDateFormat()`` method on the
47 ``core`` extension instance. The first argument is the default format for
48 dates and the second one is the default format for date intervals:
52 $twig = new Twig_Environment($loader);
53 $twig->getExtension('core')->setDateFormat('d/m/Y', '%d days');
58 By default, the date is displayed by applying the default timezone (the one
59 specified in php.ini or declared in Twig -- see below), but you can override
60 it by explicitly specifying a timezone:
64 {{ post.published_at|date("m/d/Y", "Europe/Paris") }}
66 If the date is already a DateTime object, and if you want to keep its current
67 timezone, pass ``false`` as the timezone value:
71 {{ post.published_at|date("m/d/Y", false) }}
73 The default timezone can also be set globally by calling ``setTimezone()``:
77 $twig = new Twig_Environment($loader);
78 $twig->getExtension('core')->setTimezone('Europe/Paris');
83 * ``format``: The date format
84 * ``timezone``: The date timezone
86 .. _`strtotime`: http://www.php.net/strtotime
87 .. _`DateTime`: http://www.php.net/DateTime
88 .. _`DateInterval`: http://www.php.net/DateInterval