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