Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Last active May 13, 2024 01:37
Show Gist options
  • Save james2doyle/4739742 to your computer and use it in GitHub Desktop.
Save james2doyle/4739742 to your computer and use it in GitHub Desktop.
PHP elapsed time function which takes in a timestamp and spits out a "timeago" sentence.
<?php
function elapsed_time($timestamp, $precision = 2) {
$time = time() - $timestamp;
$a = array('decade' => 315576000, 'year' => 31557600, 'month' => 2629800, 'week' => 604800, 'day' => 86400, 'hour' => 3600, 'min' => 60, 'sec' => 1);
$i = 0;
foreach($a as $k => $v) {
$$k = floor($time/$v);
if ($$k) $i++;
$time = $i >= $precision ? 0 : $time - $$k * $v;
$s = $$k > 1 ? 's' : '';
$$k = $$k ? $$k.' '.$k.$s.' ' : '';
@$result .= $$k;
}
return $result ? $result.'ago' : '1 sec to go';
}
echo elapsed_time('1234567890').'<br />'; // 3 years 5 months ago
echo elapsed_time('1234567890', 6); // 3 years 5 months 1 week 2 days 57 mins 4 secs ago
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment