Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 1 | ``format_datetime`` |
| 2 | =================== |
| 3 | |
| 4 | The ``format_datetime`` filter formats a date time: |
| 5 | |
| 6 | .. code-block:: twig |
| 7 | |
| 8 | {# Aug 7, 2019, 11:39:12 PM #} |
| 9 | {{ '2019-08-07 23:39:12'|format_datetime() }} |
| 10 | |
| 11 | You can tweak the output for the date part and the time part: |
| 12 | |
| 13 | .. code-block:: twig |
| 14 | |
| 15 | {# 23:39 #} |
| 16 | {{ '2019-08-07 23:39:12'|format_datetime('none', 'short', locale='fr') }} |
| 17 | |
| 18 | {# 07/08/2019 #} |
| 19 | {{ '2019-08-07 23:39:12'|format_datetime('short', 'none', locale='fr') }} |
| 20 | |
| 21 | {# mercredi 7 août 2019 23:39:12 UTC #} |
| 22 | {{ '2019-08-07 23:39:12'|format_datetime('full', 'full', locale='fr') }} |
| 23 | |
| 24 | Supported values are: ``none``, ``short``, ``medium``, ``long``, and ``full``. |
| 25 | |
| 26 | For greater flexiblity, you can even define your own pattern (see the `ICU user |
| 27 | guide |
| 28 | <https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax>`_ |
| 29 | for supported patterns). |
| 30 | |
| 31 | .. code-block:: twig |
| 32 | |
| 33 | {# 11 oclock PM, GMT #} |
| 34 | {{ '2019-08-07 23:39:12'|format_datetime(pattern="hh 'oclock' a, zzzz") }} |
| 35 | |
| 36 | By default, the filter uses the current locale. You can pass it explicitly: |
| 37 | |
| 38 | .. code-block:: twig |
| 39 | |
| 40 | {# 7 août 2019 23:39:12 #} |
| 41 | {{ '2019-08-07 23:39:12'|format_datetime(locale='fr') }} |
| 42 | |
| 43 | .. note:: |
| 44 | |
| 45 | The ``format_datetime`` filter is part of the ``IntlExtension`` which is not |
| 46 | installed by default. Install it first: |
| 47 | |
| 48 | .. code-block:: bash |
| 49 | |
| 50 | $ composer require twig/intl-extra |
| 51 | |
| 52 | Then, on Symfony projects, install the ``twig/extra-bundle``: |
| 53 | |
| 54 | .. code-block:: bash |
| 55 | |
| 56 | $ composer require twig/extra-bundle |
| 57 | |
| 58 | Otherwise, add the extension explicitly on the Twig environment:: |
| 59 | |
| 60 | use Twig\Extra\Intl\IntlExtension; |
| 61 | |
| 62 | $twig = new \Twig\Environment(...); |
| 63 | $twig->addExtension(new IntlExtension()); |
| 64 | |
| 65 | Arguments |
| 66 | --------- |
| 67 | |
| 68 | * ``locale``: The locale |
| 69 | * ``dateFormat``: The date format |
| 70 | * ``timeFormat``: The time format |
| 71 | * ``pattern``: A date time pattern |
| 72 | * ``timezone``: The date timezone |
| 73 | * ``calendar``: The calendar (Gregorian by default) |