Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 1 | ``date`` |
| 2 | ======== |
| 3 | |
| 4 | The ``date`` filter formats a date to a given format: |
| 5 | |
| 6 | .. code-block:: twig |
| 7 | |
| 8 | {{ post.published_at|date("m/d/Y") }} |
| 9 | |
| 10 | The format specifier is the same as supported by `date`_, |
| 11 | except when the filtered data is of type `DateInterval`_, when the format must conform to |
| 12 | `DateInterval::format`_ instead. |
| 13 | |
| 14 | The ``date`` filter accepts strings (it must be in a format supported by the |
| 15 | `strtotime`_ function), `DateTime`_ instances, or `DateInterval`_ instances. For |
| 16 | instance, to display the current date, filter the word "now": |
| 17 | |
| 18 | .. code-block:: twig |
| 19 | |
| 20 | {{ "now"|date("m/d/Y") }} |
| 21 | |
| 22 | To escape words and characters in the date format use ``\\`` in front of each |
| 23 | character: |
| 24 | |
| 25 | .. code-block:: twig |
| 26 | |
| 27 | {{ post.published_at|date("F jS \\a\\t g:ia") }} |
| 28 | |
| 29 | If the value passed to the ``date`` filter is ``null``, it will return the |
| 30 | current date by default. If an empty string is desired instead of the current |
| 31 | date, use a ternary operator: |
| 32 | |
| 33 | .. code-block:: twig |
| 34 | |
| 35 | {{ post.published_at is empty ? "" : post.published_at|date("m/d/Y") }} |
| 36 | |
| 37 | If no format is provided, Twig will use the default one: ``F j, Y H:i``. This |
| 38 | default can be changed by calling the ``setDateFormat()`` method on the |
| 39 | ``core`` extension instance. The first argument is the default format for |
| 40 | dates 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 | |
| 45 | Timezone |
| 46 | -------- |
| 47 | |
| 48 | By default, the date is displayed by applying the default timezone (the one |
| 49 | specified in php.ini or declared in Twig -- see below), but you can override |
| 50 | it by explicitly specifying a timezone: |
| 51 | |
| 52 | .. code-block:: twig |
| 53 | |
| 54 | {{ post.published_at|date("m/d/Y", "Europe/Paris") }} |
| 55 | |
| 56 | If the date is already a DateTime object, and if you want to keep its current |
| 57 | timezone, pass ``false`` as the timezone value: |
| 58 | |
| 59 | .. code-block:: twig |
| 60 | |
| 61 | {{ post.published_at|date("m/d/Y", false) }} |
| 62 | |
| 63 | The 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 | |
| 68 | Arguments |
| 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 |