*/
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, '.', ',');
/**
* 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.
*
- * @return string The default date format string
+ * @return array The default date format string and the default date interval format string
*/
public function getDateFormat()
{
- return $this->dateFormat;
+ return $this->dateFormats;
}
/**
* {{ post.published_at|date("m/d/Y") }}
* </pre>
*
- * @param Twig_Environment $env A Twig_Environment instance
- * @param DateTime|string $date A date
- * @param string $format A format
- * @param DateTimeZone|string $timezone A timezone
+ * @param Twig_Environment $env A Twig_Environment instance
+ * @param DateTime|DateInterval|string $date A date
+ * @param string $format A format
+ * @param DateTimeZone|string $timezone A timezone
*
* @return string The formatter date
*/
function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $timezone = null)
{
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) {
--TEST--
"date" filter
--TEMPLATE--
-{{ date|date }}
-{{ date|date('d/m/Y') }}
+{{ date1|date }}
+{{ date1|date('d/m/Y') }}
+{{ date2|date }}
+{{ date2|date('%d days') }}
--DATA--
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(
- 'date' => mktime(13, 45, 0, 10, 4, 2010),
+ 'date1' => mktime(13, 45, 0, 10, 4, 2010),
+ 'date2' => new DateInterval('P2D'),
)
--EXPECT--
2010-10-04
04/10/2010
+2 days 0 hours
+2 days