Skip to content

Instantly share code, notes, and snippets.

@Lejdborg
Created March 31, 2011 18:40
Show Gist options
  • Save Lejdborg/896949 to your computer and use it in GitHub Desktop.
Save Lejdborg/896949 to your computer and use it in GitHub Desktop.
Convert timestamp to "X hours since"
<?php
function get_human_time($timestamp) {
$since = time() - $timestamp;
$chunks = array(
array(60 * 60 * 24 * 365, 'year', 'years'),
array(60 * 60 * 24 * 30, 'month', 'months'),
array(60 * 60 * 24 * 7, 'week', 'weeks'),
array(60 * 60 * 24, 'day', 'days'),
array(60 * 60, 'hour', 'hours'),
array(60, 'minute', 'minutes'),
array(1, 'second', 'seconds')
);
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
$names = $chunks[$i][2];
if (($count = floor($since / $seconds)) != 0)
break;
}
$print = ($count == 1) ? '1 '.$name : $count.' '.$names;
return $print.' ago';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment