Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 1 | ``date`` |
| 2 | ======== |
| 3 | |
| 4 | Converts an argument to a date to allow date comparison: |
| 5 | |
| 6 | .. code-block:: html+twig |
| 7 | |
| 8 | {% if date(user.created_at) < date('-2days') %} |
| 9 | {# do something #} |
| 10 | {% endif %} |
| 11 | |
| 12 | The argument must be in one of PHP’s supported `date and time formats`_. |
| 13 | |
| 14 | You can pass a timezone as the second argument: |
| 15 | |
| 16 | .. code-block:: html+twig |
| 17 | |
| 18 | {% if date(user.created_at) < date('-2days', 'Europe/Paris') %} |
| 19 | {# do something #} |
| 20 | {% endif %} |
| 21 | |
| 22 | If no argument is passed, the function returns the current date: |
| 23 | |
| 24 | .. code-block:: html+twig |
| 25 | |
| 26 | {% if date(user.created_at) < date() %} |
| 27 | {# always! #} |
| 28 | {% endif %} |
| 29 | |
| 30 | .. note:: |
| 31 | |
| 32 | You can set the default timezone globally by calling ``setTimezone()`` on |
| 33 | the ``core`` extension instance:: |
| 34 | |
| 35 | $twig = new \Twig\Environment($loader); |
| 36 | $twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone('Europe/Paris'); |
| 37 | |
| 38 | Arguments |
| 39 | --------- |
| 40 | |
| 41 | * ``date``: The date |
| 42 | * ``timezone``: The timezone |
| 43 | |
| 44 | .. _`date and time formats`: https://secure.php.net/manual/en/datetime.formats.php |