mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-13 19:06:40 +00:00
changed the date filter behavior to always apply the default timezone, except if false is passed as the timezone
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
* 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)
|
||||
|
||||
+27
-11
@@ -10,6 +10,9 @@
|
||||
.. 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
|
||||
@@ -24,17 +27,20 @@ instance, to display the current date, filter the word "now":
|
||||
|
||||
{{ "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
|
||||
@@ -46,6 +52,24 @@ dates and the second one is the default format for date intervals:
|
||||
$twig = new Twig_Environment($loader);
|
||||
$twig->getExtension('core')->setDateFormat('d/m/Y', '%d days');
|
||||
|
||||
Timezone
|
||||
--------
|
||||
|
||||
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:
|
||||
|
||||
.. code-block:: jinja
|
||||
|
||||
{{ 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|date("m/d/Y", false) }}
|
||||
|
||||
The default timezone can also be set globally by calling ``setTimezone()``:
|
||||
|
||||
.. code-block:: php
|
||||
@@ -53,14 +77,6 @@ The default timezone can also be set globally by calling ``setTimezone()``:
|
||||
$twig = new Twig_Environment($loader);
|
||||
$twig->getExtension('core')->setTimezone('Europe/Paris');
|
||||
|
||||
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 is empty ? "" : post.published_at|date("m/d/Y") }}
|
||||
|
||||
.. _`strtotime`: http://www.php.net/strtotime
|
||||
.. _`DateTime`: http://www.php.net/DateTime
|
||||
.. _`DateInterval`: http://www.php.net/DateInterval
|
||||
|
||||
+11
-21
@@ -397,15 +397,6 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -424,12 +415,7 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $
|
||||
*/
|
||||
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;
|
||||
@@ -453,15 +439,19 @@ function twig_date_modify_filter(Twig_Environment $env, $date, $modifier)
|
||||
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;
|
||||
}
|
||||
@@ -469,12 +459,12 @@ function twig_date_converter(Twig_Environment $env, $date = null, $timezone = nu
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,8 +21,9 @@
|
||||
{{ 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(
|
||||
@@ -55,5 +56,6 @@ December 30, 1969 23:59
|
||||
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
|
||||
America/New_York
|
||||
|
||||
Reference in New Issue
Block a user