Skip to content

Commit

Permalink
merged branch Seldaek/date_modify (PR twigphp#757)
Browse files Browse the repository at this point in the history
Commits
-------

ae85586 Add docs for date_modify
0366cf6 Fix docs of date filter

Discussion
----------

Date modify filter

This is probably possible via `date(foo)|modify('-1day')|format()`, but it's non obvious and a dedicated filter might be a bit more prominent.

---------------------------------------------------------------------------

by fabpot at 2012-06-22T07:01:32Z

Just a quick thought: As it modifies the date, it should probably be a function, not a filter.

---------------------------------------------------------------------------

by stof at 2012-06-22T07:24:52Z

@fabpot Aren't filters meant to transform the value ?
  • Loading branch information
fabpot committed Jun 26, 2012
2 parents b7ca23f + ae85586 commit 5758be0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
4 changes: 2 additions & 2 deletions doc/filters/date.rst
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions doc/filters/date_modify.rst
@@ -0,0 +1,18 @@
``date_modify``
===============

.. versionadded:: 1.9
The date_modify filter has been added in Twig 1.9.

The ``date_modify`` filter modifies a date with a given modifier string:

.. code-block:: jinja
{{ post.published_at|date_modify("+1 day")|date("m/d/Y") }}
The ``date_modify`` filter accepts strings (it must be in a format supported by the
`strtotime`_ function) or `DateTime`_ instances. You can easily combine it with the `date`_
filter for formatting.

.. _`strtotime`: http://www.php.net/strtotime
.. _`DateTime`: http://www.php.net/DateTime
27 changes: 26 additions & 1 deletion lib/Twig/Extension/Core.php
Expand Up @@ -123,6 +123,7 @@ public function getFilters()
$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)),
Expand Down Expand Up @@ -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)
{
Expand All @@ -399,6 +400,30 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $
return twig_date_converter($env, $date, $timezone)->format($format);
}

/**
* Returns a new date object modified
*
* <pre>
* {{ post.published_at|modify("-1day")|date("m/d/Y") }}
* </pre>
*
* @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.
*
Expand Down
14 changes: 14 additions & 0 deletions 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

0 comments on commit 5758be0

Please sign in to comment.