* 1.11.0 (2012-XX-XX)
+ * changed the date filter behavior to always apply the default timezone, except if false is passed as the timezone
* fixed default timezone usage for the date function
* optimized the way Twig exceptions are managed (to make them faster)
* added Twig_ExistsLoaderInterface (implementing this interface in your loader make the chain loader much faster)
.. versionadded:: 1.6.1
The default timezone support has been added in Twig 1.6.1.
+.. versionadded:: 1.11.0
+ The introduction of the false value for the timezone was introduced in Twig 1.11.0
+
The ``date`` filter formats a date to a given format:
.. code-block:: jinja
{{ "now"|date("m/d/Y") }}
-To escape words and characters in the date format use ``\\`` in front of each character:
+To escape words and characters in the date format use ``\\`` in front of each
+character:
.. code-block:: jinja
{{ post.published_at|date("F jS \\a\\t g:ia") }}
-You can also specify a timezone:
+If the value passed to the ``date`` filter is ``null``, it will return the
+current date by default. If an empty string is desired instead of the current
+date, use a ternary operator:
.. code-block:: jinja
- {{ post.published_at|date("m/d/Y", "Europe/Paris") }}
+ {{ post.published_at is empty ? "" : post.published_at|date("m/d/Y") }}
If no format is provided, Twig will use the default one: ``F j, Y H:i``. This
default can be easily changed by calling the ``setDateFormat()`` method on the
$twig = new Twig_Environment($loader);
$twig->getExtension('core')->setDateFormat('d/m/Y', '%d days');
-The default timezone can also be set globally by calling ``setTimezone()``:
+Timezone
+--------
-.. code-block:: php
+By default, the date is displayed by applying the default timezone (the one
+specified in php.ini or declared in Twig -- see below), but you can override
+it by explicitly specifying a timezone:
- $twig = new Twig_Environment($loader);
- $twig->getExtension('core')->setTimezone('Europe/Paris');
+.. code-block:: jinja
-If the value passed to the ``date`` filter is ``null``, it will return the
-current date by default. If an empty string is desired instead of the current
-date, use a ternary operator:
+ {{ post.published_at|date("m/d/Y", "Europe/Paris") }}
+
+If the date is already a DateTime object, and if you want to keep its current
+timezone, pass ``false`` as the timezone value:
.. code-block:: jinja
- {{ post.published_at is empty ? "" : post.published_at|date("m/d/Y") }}
+ {{ post.published_at|date("m/d/Y", false) }}
+
+The default timezone can also be set globally by calling ``setTimezone()``:
+
+.. code-block:: php
+
+ $twig = new Twig_Environment($loader);
+ $twig->getExtension('core')->setTimezone('Europe/Paris');
.. _`strtotime`: http://www.php.net/strtotime
.. _`DateTime`: http://www.php.net/DateTime
return $date->format($format);
}
- if ($date instanceof DateTime) {
- if (null !== $timezone) {
- $date = clone $date;
- $date->setTimezone($timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone));
- }
-
- return $date->format($format);
- }
-
return twig_date_converter($env, $date, $timezone)->format($format);
}
*/
function twig_date_modify_filter(Twig_Environment $env, $date, $modifier)
{
- if ($date instanceof DateTime) {
- $date = clone $date;
- } else {
- $date = twig_date_converter($env, $date);
- }
-
+ $date = twig_date_converter($env, $date, false);
$date->modify($modifier);
return $date;
function twig_date_converter(Twig_Environment $env, $date = null, $timezone = null)
{
// determine the timezone
- if (null === $timezone) {
- $timezone = $env->getExtension('core')->getTimezone();
+ if (!$timezone) {
+ $defaultTimezone = $env->getExtension('core')->getTimezone();
} elseif (!$timezone instanceof DateTimeZone) {
- $timezone = new DateTimeZone($timezone);
+ $defaultTimezone = new DateTimeZone($timezone);
+ } else {
+ $defaultTimezone = $timezone;
}
if ($date instanceof DateTime) {
$date = clone $date;
- $date->setTimezone($timezone);
+ if (false !== $timezone) {
+ $date->setTimezone($defaultTimezone);
+ }
return $date;
}
$asString = (string) $date;
if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) {
$date = new DateTime('@'.$date);
- $date->setTimezone($timezone);
+ $date->setTimezone($defaultTimezone);
return $date;
}
- return new DateTime($date, $timezone);
+ return new DateTime($date, $defaultTimezone);
}
/**
{{ date5|date('d/m/Y') }}
{{ date6|date('d/m/Y H:i:s P', 'Europe/Paris') }}
{{ date6|date('d/m/Y H:i:s P', 'Asia/Hong_Kong') }}
+{{ date6|date('d/m/Y H:i:s P', false) }}
{{ date6|date('e', 'Europe/Paris') }}
-{{ date6|date('e') }}
+{{ date6|date('e', false) }}
--DATA--
date_default_timezone_set('UTC');
return array(
30/12/1969
04/10/2010 19:45:00 +02:00
05/10/2010 01:45:00 +08:00
+04/10/2010 13:45:00 -04:00
Europe/Paris
-America/New_York
\ No newline at end of file
+America/New_York