From: Fabien Potencier Date: Thu, 27 May 2010 16:29:40 +0000 (+0200) Subject: changed the date filter to support any date format supported by DateTime (closes... X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=1847634d0c5208b5a3da488104013f57eafda126;p=web%2Fkonrad%2Ftwig.git changed the date filter to support any date format supported by DateTime (closes #56) --- diff --git a/CHANGELOG b/CHANGELOG index 24e53d8..225ca4e 100644 --- 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 diff --git a/doc/02-Twig-for-Template-Designers.markdown b/doc/02-Twig-for-Template-Designers.markdown index 9277920..eff8116 100644 --- a/doc/02-Twig-for-Template-Designers.markdown +++ b/doc/02-Twig-for-Template-Designers.markdown @@ -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` diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 04bba6f..be92d94 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -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) diff --git a/test/fixtures/filters/date.test b/test/fixtures/filters/date.test index 0a388f9..21b57ba 100644 --- a/test/fixtures/filters/date.test +++ b/test/fixtures/filters/date.test @@ -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