Created
May 1, 2012 18:15
-
-
Save paulredmond/2570188 to your computer and use it in GitHub Desktop.
Smarty time ago function
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 | |
function smarty_function_timeago( $params, &$smarty ) | |
{ | |
$conversions = array( | |
'millisecond' => 1, | |
'second' => 1000, | |
'minute' => 60, | |
'hour' => 60, | |
'day' => 24, | |
'month' => 30, | |
'year' => 12 | |
); | |
# Set up params. | |
$threshold = isset($params['threshold']) ? $params['threshold'] : 0; | |
$now = strtotime('now'); | |
$time = strtotime($params['time']); | |
$delta = ($now - $time) * 1000; | |
if ($delta <= $threshold) { | |
return "Just now"; | |
} | |
foreach ($conversions as $key => $val) { | |
if($delta < $conversions[$key]) { | |
break; | |
} else { | |
$units = $key; | |
$delta = $delta / $conversions[$key]; | |
} | |
} | |
$delta = floor($delta); | |
if($delta != 1) { $units .= 's'; } | |
return implode(' ', array($delta, $units, 'ago')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment