Skip to content

Instantly share code, notes, and snippets.

@mandb
Created December 3, 2013 19:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandb/7776326 to your computer and use it in GitHub Desktop.
Save mandb/7776326 to your computer and use it in GitHub Desktop.
infinitely extendable time variation with adjustable precision
/* simple-text output for time periods measured in seconds */
function timeAgo($seconds, $precision=2){
$i[1]='s';
$i[60]='m';
$i[3600]='h';
$i[86400]='d';
$i[31536000]='y';
krsort($i);
foreach($i as $timeInterval=>$shortcode){
if ($seconds > $timeInterval){
$precision--;
$out .= floor($seconds/$timeInterval) . $shortcode;
$seconds -= floor($seconds/$timeInterval) * $timeInterval;
}
if ($precision <=0){
break;
}
}
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment