Skip to content

Instantly share code, notes, and snippets.

@mihai-vlc
Last active December 19, 2015 11:08
Show Gist options
  • Save mihai-vlc/5945038 to your computer and use it in GitHub Desktop.
Save mihai-vlc/5945038 to your computer and use it in GitHub Desktop.
PHP: time since
<?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