From 0366cf6185ce575e8cbd8354880a1f95743505b3 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 20 Jun 2012 19:45:27 +0200 Subject: [PATCH] Fix docs of date filter --- doc/filters/date.rst | 4 +- lib/Twig/Extension/Core.php | 27 ++++++++++++++++++++- test/Twig/Tests/Fixtures/filters/date_modify.test | 14 +++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/filters/date_modify.test diff --git a/doc/filters/date.rst b/doc/filters/date.rst index 9833f03..e6dc613 100644 --- a/doc/filters/date.rst +++ b/doc/filters/date.rst @@ -17,7 +17,7 @@ The ``date`` filter formats a date to a given format: {{ post.published_at|date("m/d/Y") }} The ``date`` filter accepts strings (it must be in a format supported by the -`date`_ function), `DateTime`_ instances, or `DateInterval`_ instances. For +`strtotime`_ function), `DateTime`_ instances, or `DateInterval`_ instances. For instance, to display the current date, filter the word "now": .. code-block:: jinja @@ -53,7 +53,7 @@ The default timezone can also be set globally by calling ``setTimezone()``: $twig = new Twig_Environment($loader); $twig->getExtension('core')->setTimezone('Europe/Paris'); -.. _`date`: http://www.php.net/date +.. _`strtotime`: http://www.php.net/strtotime .. _`DateTime`: http://www.php.net/DateTime .. _`DateInterval`: http://www.php.net/DateInterval diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 27c80ce..9a2926a 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -123,6 +123,7 @@ class Twig_Extension_Core extends Twig_Extension $filters = array( // formatting filters 'date' => new Twig_Filter_Function('twig_date_format_filter', array('needs_environment' => true)), + 'date_modify' => new Twig_Filter_Function('twig_date_modify_filter', array('needs_environment' => true)), 'format' => new Twig_Filter_Function('sprintf'), 'replace' => new Twig_Filter_Function('strtr'), 'number_format' => new Twig_Filter_Function('twig_number_format_filter', array('needs_environment' => true)), @@ -378,7 +379,7 @@ function twig_random(Twig_Environment $env, $values = null) * @param string $format A format * @param DateTimeZone|string $timezone A timezone * - * @return string The formatter date + * @return string The formatted date */ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $timezone = null) { @@ -400,6 +401,30 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $ } /** + * Returns a new date object modified + * + *
+ *   {{ post.published_at|modify("-1day")|date("m/d/Y") }}
+ * 
+ * + * @param Twig_Environment $env A Twig_Environment instance + * @param DateTime|string $date A date + * @param string $modifier A modifier string + * + * @return DateTime A new date object + */ +function twig_date_modify_filter(Twig_Environment $env, $date, $modifier) +{ + if ($date instanceof DateTime) { + $date = clone $date; + } else { + $date = twig_date_converter($env, $date); + } + + return $date->modify($modifier); +} + +/** * Converts an input to a DateTime instance. * *
diff --git a/test/Twig/Tests/Fixtures/filters/date_modify.test b/test/Twig/Tests/Fixtures/filters/date_modify.test
new file mode 100644
index 0000000..53d3a69
--- /dev/null
+++ b/test/Twig/Tests/Fixtures/filters/date_modify.test
@@ -0,0 +1,14 @@
+--TEST--
+"date_modify" filter
+--TEMPLATE--
+{{ date1|date_modify('-1day')|date('Y-m-d H:i:s') }}
+{{ date2|date_modify('-1day')|date('Y-m-d H:i:s') }}
+--DATA--
+date_default_timezone_set('UTC');
+return array(
+    'date1' => '2010-10-04 13:45',
+    'date2' => new DateTime('2010-10-04 13:45'),
+)
+--EXPECT--
+2010-10-03 13:45:00
+2010-10-03 13:45:00
-- 
1.7.2.5