Readable Time – PHP, given a UNIX timestamp, return a readable time
# Given a unix timestamp, returns in readable time ie "1 day, 7 hours, 23 minutes, 2 seconds"
function readable_time($timestamp, $num_times = 2)
{
//this returns human readable time when it was uploaded (array in seconds)
$times = array(31536000 => 'year', 2592000 => 'month', 604800 => 'week',
86400 => 'day', 3600 => 'hour', 60 => 'min', 1 => 'sec');
$now = time();
$secs = $timestamp;
//Fix so that something is always displayed
if ($secs == 0)
{
$secs = 1;
}
$count = 0;
$time = '';
foreach ($times AS $key => $value)
{
if ($secs >= $key)
{
//time found
$s = '';
$time .= floor($secs / $key);
if ((floor($secs / $key) != 1))
{
$s = 's';
}
$time .= ' ' . $value . $s;
$count++;
$secs = $secs % $key;
if ($count > $num_times - 1 || $secs == 0)
{
break;
}
else
{
$time .= ', ';
}
}
}
return $time;
}