mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-21 15:48:42 +00:00
added a way to set the default global date interval format
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
* 1.5.0-RC2
|
* 1.5.0-RC2
|
||||||
|
|
||||||
|
* added a way to set the default global date interval format
|
||||||
* fixed the date filter for DateInterval instances (setTimezone() does not exist for them)
|
* fixed the date filter for DateInterval instances (setTimezone() does not exist for them)
|
||||||
* refactored Twig_Template::display() to ease its extension
|
* refactored Twig_Template::display() to ease its extension
|
||||||
* added a number_format filter
|
* added a number_format filter
|
||||||
|
|||||||
+19
-11
@@ -14,27 +14,34 @@ if (!defined('ENT_SUBSTITUTE')) {
|
|||||||
*/
|
*/
|
||||||
class Twig_Extension_Core extends Twig_Extension
|
class Twig_Extension_Core extends Twig_Extension
|
||||||
{
|
{
|
||||||
protected $dateFormat = 'F j, Y H:i';
|
protected $dateFormats = array('F j, Y H:i', '%d days');
|
||||||
protected $numberFormat = array(0, '.', ',');
|
protected $numberFormat = array(0, '.', ',');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the default format to be used by the date filter.
|
* Sets the default format to be used by the date filter.
|
||||||
*
|
*
|
||||||
* @param string $format The default date format string
|
* @param string $format The default date format string
|
||||||
|
* @param string $dateIntervalFormat The default date interval format string
|
||||||
*/
|
*/
|
||||||
public function setDateFormat($format)
|
public function setDateFormat($format = null, $dateIntervalFormat = null)
|
||||||
{
|
{
|
||||||
$this->dateFormat = $format;
|
if (null !== $format) {
|
||||||
|
$this->dateFormats[0] = $format;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null !== $dateIntervalFormat) {
|
||||||
|
$this->dateFormats[1] = $dateIntervalFormat;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the default format to be used by the date filter.
|
* Gets the default format to be used by the date filter.
|
||||||
*
|
*
|
||||||
* @return string The default date format string
|
* @return array The default date format string and the default date interval format string
|
||||||
*/
|
*/
|
||||||
public function getDateFormat()
|
public function getDateFormat()
|
||||||
{
|
{
|
||||||
return $this->dateFormat;
|
return $this->dateFormats;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -300,17 +307,18 @@ function twig_random($values)
|
|||||||
* {{ post.published_at|date("m/d/Y") }}
|
* {{ post.published_at|date("m/d/Y") }}
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param Twig_Environment $env A Twig_Environment instance
|
* @param Twig_Environment $env A Twig_Environment instance
|
||||||
* @param DateTime|string $date A date
|
* @param DateTime|DateInterval|string $date A date
|
||||||
* @param string $format A format
|
* @param string $format A format
|
||||||
* @param DateTimeZone|string $timezone A timezone
|
* @param DateTimeZone|string $timezone A timezone
|
||||||
*
|
*
|
||||||
* @return string The formatter date
|
* @return string The formatter date
|
||||||
*/
|
*/
|
||||||
function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $timezone = null)
|
function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $timezone = null)
|
||||||
{
|
{
|
||||||
if (null === $format) {
|
if (null === $format) {
|
||||||
$format = $env->getExtension('core')->getDateFormat();
|
$formats = $env->getExtension('core')->getDateFormat();
|
||||||
|
$format = $date instanceof DateInterval ? $formats[1] : $formats[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($date instanceof DateInterval || $date instanceof DateTime) {
|
if ($date instanceof DateInterval || $date instanceof DateTime) {
|
||||||
|
|||||||
@@ -11,6 +11,8 @@
|
|||||||
{{ date4|date('d/m/Y') }}
|
{{ date4|date('d/m/Y') }}
|
||||||
{{ date5|date }}
|
{{ date5|date }}
|
||||||
{{ date5|date('d/m/Y') }}
|
{{ date5|date('d/m/Y') }}
|
||||||
|
{{ date6|date }}
|
||||||
|
{{ date6|date('%d days %h hours') }}
|
||||||
--DATA--
|
--DATA--
|
||||||
date_default_timezone_set('UTC');
|
date_default_timezone_set('UTC');
|
||||||
return array(
|
return array(
|
||||||
@@ -19,6 +21,7 @@ return array(
|
|||||||
'date3' => '2010-10-04 13:45',
|
'date3' => '2010-10-04 13:45',
|
||||||
'date4' => 1286199900,
|
'date4' => 1286199900,
|
||||||
'date5' => -86410,
|
'date5' => -86410,
|
||||||
|
'date6' => new DateInterval('P2D'),
|
||||||
)
|
)
|
||||||
--EXPECT--
|
--EXPECT--
|
||||||
October 4, 2010 13:45
|
October 4, 2010 13:45
|
||||||
@@ -31,3 +34,5 @@ October 4, 2010 13:45
|
|||||||
04/10/2010
|
04/10/2010
|
||||||
December 30, 1969 23:59
|
December 30, 1969 23:59
|
||||||
30/12/1969
|
30/12/1969
|
||||||
|
2 days
|
||||||
|
2 days 0 hours
|
||||||
|
|||||||
@@ -1,14 +1,19 @@
|
|||||||
--TEST--
|
--TEST--
|
||||||
"date" filter
|
"date" filter
|
||||||
--TEMPLATE--
|
--TEMPLATE--
|
||||||
{{ date|date }}
|
{{ date1|date }}
|
||||||
{{ date|date('d/m/Y') }}
|
{{ date1|date('d/m/Y') }}
|
||||||
|
{{ date2|date }}
|
||||||
|
{{ date2|date('%d days') }}
|
||||||
--DATA--
|
--DATA--
|
||||||
date_default_timezone_set('UTC');
|
date_default_timezone_set('UTC');
|
||||||
$twig->getExtension('core')->setDateFormat('Y-m-d');
|
$twig->getExtension('core')->setDateFormat('Y-m-d', '%d days %h hours');
|
||||||
return array(
|
return array(
|
||||||
'date' => mktime(13, 45, 0, 10, 4, 2010),
|
'date1' => mktime(13, 45, 0, 10, 4, 2010),
|
||||||
|
'date2' => new DateInterval('P2D'),
|
||||||
)
|
)
|
||||||
--EXPECT--
|
--EXPECT--
|
||||||
2010-10-04
|
2010-10-04
|
||||||
04/10/2010
|
04/10/2010
|
||||||
|
2 days 0 hours
|
||||||
|
2 days
|
||||||
|
|||||||
Reference in New Issue
Block a user