Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactored date filter code

  • Loading branch information...
commit 519b6805b043c6c82338b4e534d7c2b2c7a638b9 1 parent bd08072
@fabpot authored
Showing with 18 additions and 15 deletions.
  1. +1 −0  CHANGELOG
  2. +17 −15 lib/Twig/Extension/Core.php
View
1  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
View
32 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);
Please sign in to comment.
Something went wrong with that request. Please try again.