Last active
August 29, 2015 14:04
-
-
Save saro0h/0edb92b5f6da60d08dc4 to your computer and use it in GitHub Desktop.
Twig filer extension to compute how long from a date.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Bundle\DemoBundle\Twig; | |
class TimeAgoExtension extends \Twig_Extension | |
{ | |
public function getFilters() | |
{ | |
return array( | |
new \Twig_SimpleFilter('time_ago', array($this, 'timeAgoFilter')), | |
); | |
} | |
/** | |
* @see https://github.com/symfony/symfony1/blob/1.4/lib/helper/DateHelper.php | |
*/ | |
public function timeAgoFilter($fromTime, $toTime = null, $include_seconds = false) | |
{ | |
if ($fromTime instanceof \DateTime) { | |
$fromTime = $fromTime->format('U'); | |
} | |
if ($toTime instanceof \DateTime) { | |
$toTime = $toTime->format('U'); | |
} | |
$toTime = $toTime ? $toTime: time(); | |
$distanceInMinutes = floor(abs($toTime - $fromTime) / 60); | |
$distanceInSeconds = floor(abs($toTime - $fromTime)); | |
$string = ''; | |
$parameters = array(); | |
if ($distanceInMinutes <= 1) | |
{ | |
if (!$include_seconds) { | |
$string = $distanceInMinutes == 0 ? 'less than a minute' : '1 minute'; | |
} else { | |
if ($distanceInSeconds <= 5) { | |
$string = 'less than 5 seconds'; | |
} else if ($distanceInSeconds >= 6 && $distanceInSeconds <= 10) { | |
$string = 'less than 10 seconds'; | |
} else if ($distanceInSeconds >= 11 && $distanceInSeconds <= 20) { | |
$string = 'less than 20 seconds'; | |
} else if ($distanceInSeconds >= 21 && $distanceInSeconds <= 40) { | |
$string = 'half a minute'; | |
} else if ($distanceInSeconds >= 41 && $distanceInSeconds <= 59) { | |
$string = 'less than a minute'; | |
} else { | |
$string = '1 minute'; | |
} | |
} | |
} else if ($distanceInMinutes >= 2 && $distanceInMinutes <= 44) { | |
$string = '%minutes% minutes'; | |
$parameters['%minutes%'] = $distanceInMinutes; | |
} else if ($distanceInMinutes >= 45 && $distanceInMinutes <= 89) { | |
$string = 'about 1 hour'; | |
} else if ($distanceInMinutes >= 90 && $distanceInMinutes <= 1439) { | |
$string = 'about %hours% hours'; | |
$parameters['%hours%'] = round($distanceInMinutes / 60); | |
} else if ($distanceInMinutes >= 1440 && $distanceInMinutes <= 2879) { | |
$string = '1 day'; | |
} else if ($distanceInMinutes >= 2880 && $distanceInMinutes <= 43199) { | |
$string = '%days% days'; | |
$parameters['%days%'] = round($distanceInMinutes / 1440); | |
} else if ($distanceInMinutes >= 43200 && $distanceInMinutes <= 86399) { | |
$string = 'about 1 month'; | |
} else if ($distanceInMinutes >= 86400 && $distanceInMinutes <= 525959) { | |
$string = '%months% months'; | |
$parameters['%months%'] = round($distanceInMinutes / 43200); | |
} else if ($distanceInMinutes >= 525960 && $distanceInMinutes <= 1051919) { | |
$string = 'about 1 year'; | |
} else { | |
$string = 'over %years% years'; | |
$parameters['%years%'] = floor($distanceInMinutes / 525960); | |
} | |
return strtr($string, $parameters); | |
} | |
public function getName() | |
{ | |
return 'time_ago_extension'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment