Working with dates
There are 3 Twig functions for working with dates:
- date() function , which is used to get the current date, or convert a string into a date.
- date() filter , which is used to format a date.
- date_modify() filter , which is used to modify a date.
date()
utilizes the formatting parameters found here: http://php.net/manual/en/function.date.php
You could use the date filter to format a date in a variable:
{% set first_action = '2012-12-13 12:22:33' %}
{{ first_action | date("F j, Y") }}
{#Results in: December 13, 2012 #}
Using date()
as a function without passing it arguments results the current date, which can be similarly formatted:
{{ date() | date("F j, Y") }}
If you wanted to have a date that is 7 days from now:
{{ date() | date_modify("+7 day") | date("m/d/Y") }}
You could also localize a date according to timezone.
To check what timezone your server dates are in in the first place, run:
{{ date() | date("e") }}
This will likely be either UTC
or US/Eastern
.
If you wanted to localize a date (say, some_date_string
to be in Parisian time, you could do:
{{ some_date_string | date("Y-m-d h:m:s", "Europe/Paris") }}
Updated less than a minute ago