Skip to content

Instantly share code, notes, and snippets.

@Sergic
Created September 4, 2012 17:49
Show Gist options
  • Save Sergic/3624087 to your computer and use it in GitHub Desktop.
Save Sergic/3624087 to your computer and use it in GitHub Desktop.
date twig extension (today, yesterday, tomorrow)
<?php
/*
* This file is part of the INB application.
*
* (c) Sergey Gerdel <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace INB\CoreBundle\Twig;
use Twig_Extension;
use Twig_Filter_Method;
class StringDateExtension extends Twig_Extension
{
public function getFilters()
{
return array(
'string_date' => new Twig_Filter_Method($this, 'processFilter'),
);
}
public function processFilter(\DateTime $date, $format = 'd-m-Y')
{
$today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
$today_end = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
$u_date = $date->format('U');
$nextday = $today_end + 1;
if($u_date >= $nextday && $nextday < $nextday + (3600*24)){
return 'tomorrow';
}
elseif ($u_date >= $today && $u_date < $today_end){
return 'today';
}
elseif($u_date < $today && $u_date > $today - (3600*24)){
return 'yesterday';
}
else{
return $date->format($format);
}
}
public function getName()
{
return 'string_date_extension';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment