<?php
class DateIntervalFormat
{
/**
* Format an interval value with the requested granularity.
*
* @param integer $timestamp The length of the interval in seconds.
* @param integer $granularity How many different units to display in the string.
* @return string A string representation of the interval.
*/
public function getInterval($timestamp, $granularity = 2)
{
$seconds = time() - $timestamp;
$units = array(
'1 year|:count years' => 31536000,
'1 week|:count weeks' => 604800,
'1 day|:count days' => 86400,
'1 hour|:count hours' => 3600,
'1 min|:count min' => 60,
'1 sec|:count sec' => 1);
$output = '';
foreach ($units as $key => $value) {
$key = explode('|', $key);
if ($seconds >= $value) {
$count = floor($seconds / $value);
$output .= ($output ? ' ' : '');
$output .= ($count == 1) ? $key[0] : str_replace(':count', $count, $key[1]);
$seconds %= $value;
$granularity--;
}
if ($granularity == 0) {
break;
}
}
return $output ? $output : '0 sec';
}
}
$dateFormat = new DateIntervalFormat();
$timestamp = strtotime('2006-06-21 20:46:11');
print sprintf('Submitted %s ago', $dateFormat->getInterval($timestamp));