Yaffs site version 1.1
[yaffs-website] / vendor / twig / twig / doc / filters / date.rst
1 ``date``
2 ========
3
4 .. versionadded:: 1.1
5     The timezone support has been added in Twig 1.1.
6
7 .. versionadded:: 1.5
8     The default date format support has been added in Twig 1.5.
9
10 .. versionadded:: 1.6.1
11     The default timezone support has been added in Twig 1.6.1.
12
13 .. versionadded:: 1.11.0
14     The introduction of the false value for the timezone was introduced in Twig 1.11.0
15
16 The ``date`` filter formats a date to a given format:
17
18 .. code-block:: jinja
19
20     {{ post.published_at|date("m/d/Y") }}
21
22 The format specifier is the same as supported by `date`_,
23 except when the filtered data is of type `DateInterval`_, when the format must conform to
24 `DateInterval::format`_ instead.
25
26 The ``date`` filter accepts strings (it must be in a format supported by the
27 `strtotime`_ function), `DateTime`_ instances, or `DateInterval`_ instances. For
28 instance, to display the current date, filter the word "now":
29
30 .. code-block:: jinja
31
32     {{ "now"|date("m/d/Y") }}
33
34 To escape words and characters in the date format use ``\\`` in front of each
35 character:
36
37 .. code-block:: jinja
38
39     {{ post.published_at|date("F jS \\a\\t g:ia") }}
40
41 If the value passed to the ``date`` filter is ``null``, it will return the
42 current date by default. If an empty string is desired instead of the current
43 date, use a ternary operator:
44
45 .. code-block:: jinja
46
47     {{ post.published_at is empty ? "" : post.published_at|date("m/d/Y") }}
48
49 If no format is provided, Twig will use the default one: ``F j, Y H:i``. This
50 default can be easily changed by calling the ``setDateFormat()`` method on the
51 ``core`` extension instance. The first argument is the default format for
52 dates and the second one is the default format for date intervals:
53
54 .. code-block:: php
55
56     $twig = new Twig_Environment($loader);
57     $twig->getExtension('Twig_Extension_Core')->setDateFormat('d/m/Y', '%d days');
58
59     // before Twig 1.26
60     $twig->getExtension('core')->setDateFormat('d/m/Y', '%d days');
61
62 Timezone
63 --------
64
65 By default, the date is displayed by applying the default timezone (the one
66 specified in php.ini or declared in Twig -- see below), but you can override
67 it by explicitly specifying a timezone:
68
69 .. code-block:: jinja
70
71     {{ post.published_at|date("m/d/Y", "Europe/Paris") }}
72
73 If the date is already a DateTime object, and if you want to keep its current
74 timezone, pass ``false`` as the timezone value:
75
76 .. code-block:: jinja
77
78     {{ post.published_at|date("m/d/Y", false) }}
79
80 The default timezone can also be set globally by calling ``setTimezone()``:
81
82 .. code-block:: php
83
84     $twig = new Twig_Environment($loader);
85     $twig->getExtension('Twig_Extension_Core')->setTimezone('Europe/Paris');
86
87     // before Twig 1.26
88     $twig->getExtension('core')->setTimezone('Europe/Paris');
89
90 Arguments
91 ---------
92
93 * ``format``:   The date format
94 * ``timezone``: The date timezone
95
96 .. _`strtotime`:            http://www.php.net/strtotime
97 .. _`DateTime`:             http://www.php.net/DateTime
98 .. _`DateInterval`:         http://www.php.net/DateInterval
99 .. _`date`:                 http://www.php.net/date
100 .. _`DateInterval::format`: http://www.php.net/DateInterval.format