Created
December 3, 2013 19:52
-
-
Save mandb/7776326 to your computer and use it in GitHub Desktop.
infinitely extendable time variation with adjustable precision
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
/* 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