changed the date filter to support any date format supported by DateTime (closes...
authorFabien Potencier <fabien.potencier@gmail.com>
Thu, 27 May 2010 16:29:40 +0000 (18:29 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Thu, 27 May 2010 16:32:02 +0000 (18:32 +0200)
CHANGELOG
doc/02-Twig-for-Template-Designers.markdown
lib/Twig/Extension/Core.php
test/fixtures/filters/date.test

index 24e53d8..225ca4e 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -3,6 +3,7 @@
 Backward incompatibilities:
  * The short notation of the `block` tag changed.
 
+ * changed the date filter to support any date format supported by DateTime
  * added ignore_invalid_variables setting to throw an exception when an invalid variable is used in a template (disabled automatically when debug is true)
  * added the lexer, parser, and compiler as arguments to the Twig_Environment constructor
  * changed the cache option to only accepts an explicit path to a cache directory or false
index 9277920..eff8116 100644 (file)
@@ -898,7 +898,9 @@ The `date` filter is able to format a date to a given format:
     [twig]
     {{ post.published_at|date("m/d/Y") }}
 
-The `date` filter accepts both timestamps and `DateTime` instances.
+The `date` filter accepts any date format supported by
+[`DateTime`](http://www.php.net/manual/en/datetime.construct.php) and
+`DateTime` instances.
 
 ### `format`
 
index 04bba6f..be92d94 100644 (file)
@@ -107,9 +107,13 @@ class Twig_Extension_Core extends Twig_Extension
     }
 }
 
-function twig_date_format_filter($timestamp, $format = 'F j, Y H:i')
+function twig_date_format_filter($date, $format = 'F j, Y H:i')
 {
-    return $timestamp instanceof DateTime ? $timestamp->format($format) : date($format, $timestamp);
+    if (!$date instanceof DateTime) {
+        $date = new DateTime((ctype_digit($date) ? '@' : '').$date);
+    }
+
+    return $date->format($format);
 }
 
 function twig_urlencode_filter($url, $raw = false)
index 0a388f9..21b57ba 100644 (file)
@@ -5,10 +5,19 @@
 {{ date1|date('d/m/Y') }}
 {{ date2|date }}
 {{ date2|date('d/m/Y') }}
+{{ date3|date }}
+{{ date3|date('d/m/Y') }}
+{{ date4|date }}
+{{ date4|date('d/m/Y') }}
 --DATA--
-return array('date1' => mktime(13, 45, 0, 10, 4, 2010), 'date2' => new DateTime('2010-10-04 13:45'))
+date_default_timezone_set('UTC');
+return array('date1' => mktime(13, 45, 0, 10, 4, 2010), 'date2' => new DateTime('2010-10-04 13:45'), 'date3' => '2010-10-04 13:45', 'date4' => 1286199900)
 --EXPECT--
 October 4, 2010 13:45
 04/10/2010
 October 4, 2010 13:45
 04/10/2010
+October 4, 2010 13:45
+04/10/2010
+October 4, 2010 13:45
+04/10/2010