From: Fabien Potencier Date: Fri, 30 Dec 2011 07:58:12 +0000 (+0100) Subject: refactored date filter code X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=519b6805b043c6c82338b4e534d7c2b2c7a638b9;p=web%2Fkonrad%2Ftwig.git refactored date filter code --- diff --git a/CHANGELOG b/CHANGELOG index 777ab65..8c742cf 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.5.0-RC2 + * fixed the date filter for DateInterval instances (setTimezone() does not exist for them) * refactored Twig_Template::display() to ease its extension * added a number_format filter diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index f521523..a03156b 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -313,24 +313,26 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $ $format = $env->getExtension('core')->getDateFormat(); } - if (!$date instanceof DateInterval) { - if (!$date instanceof DateTime) { - $asString = (string) $date; - if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) { - $date = new DateTime('@'.$date); - $date->setTimezone(new DateTimeZone(date_default_timezone_get())); - } else { - $date = new DateTime($date); - } - } + if ($date instanceof DateInterval || $date instanceof DateTime) { + return $date->format($format); + } - if (null !== $timezone) { - if (!$timezone instanceof DateTimeZone) { - $timezone = new DateTimeZone($timezone); - } + // convert to a DateTime + $asString = (string) $date; + if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) { + $date = new DateTime('@'.$date); + $date->setTimezone(new DateTimeZone(date_default_timezone_get())); + } else { + $date = new DateTime($date); + } - $date->setTimezone($timezone); + // set Timezone + if (null !== $timezone) { + if (!$timezone instanceof DateTimeZone) { + $timezone = new DateTimeZone($timezone); } + + $date->setTimezone($timezone); } return $date->format($format);