Last active
December 19, 2015 11:08
-
-
Save mihai-vlc/5945038 to your computer and use it in GitHub Desktop.
PHP: time since
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 | |
/** | |
* Time elapes since a times | |
* @param int $time The past time | |
* @return string time elapssed | |
* credits: http://stackoverflow.com/a/2916189/1579481 | |
*/ | |
function tsince($time, $end_msg = 'ago') { | |
$time = abs(time() - $time); // to get the time since that moment | |
if($time == 0) | |
return "Just now"; | |
$tokens = array ( | |
31536000 => 'year', | |
2592000 => 'month', | |
604800 => 'week', | |
86400 => 'day', | |
3600 => 'hour', | |
60 => 'minute', | |
1 => 'second' | |
); | |
foreach ($tokens as $unit => $text) { | |
if ($time < $unit) continue; | |
$numberOfUnits = floor($time / $unit); | |
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' '. $end_msg; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment