1``format_datetime`` 2=================== 3 4.. versionadded:: 2.12 5 The ``format_datetime`` filter was added in Twig 2.12. 6 7The ``format_datetime`` filter formats a date time: 8 9 public function formatDateTime(Environment $env, $date, ?string $dateFormat = 'medium', ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', string $locale = null): string 10 11.. code-block:: twig 12 13 {# Aug 7, 2019, 11:39:12 PM #} 14 {{ '2019-08-07 23:39:12'|format_datetime() }} 15 16You can tweak the output for the date part and the time part: 17 18.. code-block:: twig 19 20 {# 23:39 #} 21 {{ '2019-08-07 23:39:12'|format_datetime('none', 'short', locale='fr') }} 22 23 {# 07/08/2019 #} 24 {{ '2019-08-07 23:39:12'|format_datetime('short', 'none', locale='fr') }} 25 26 {# mercredi 7 août 2019 23:39:12 UTC #} 27 {{ '2019-08-07 23:39:12'|format_datetime('full', 'full', locale='fr') }} 28 29Supported values are: ``none``, ``short``, ``medium``, ``long``, and ``full``. 30 31For greater flexiblity, you can even define your own pattern: 32 33.. code-block:: twig 34 35 {# 11 oclock PM, GMT #} 36 {{ '2019-08-07 23:39:12'|format_datetime(pattern="hh 'oclock' a, zzzz") }} 37 38By default, the filter uses the current locale. You can pass it explicitly: 39 40.. code-block:: twig 41 42 {# 7 août 2019 23:39:12 #} 43 {{ '2019-08-07 23:39:12'|format_datetime(locale='fr') }} 44 45.. note:: 46 47 The ``format_datetime`` filter is part of the ``IntlExtension`` which is not 48 installed by default. Install it first: 49 50 .. code-block:: bash 51 52 $ composer req twig/intl-extra 53 54 Then, use the ``twig/extra-bundle`` on Symfony projects or add the extension 55 explicitly on the Twig environment:: 56 57 use Twig\Extra\Intl\IntlExtension; 58 59 $twig = new \Twig\Environment(...); 60 $twig->addExtension(new IntlExtension()); 61 62Arguments 63--------- 64 65* ``locale``: The locale 66* ``dateFormat``: The date format 67* ``timeFormat``: The time format 68* ``pattern``: A date time pattern 69