You often see that some article is posted '3 days ago', '7 months ago', '1 year ago', etc. This function just gives you an example.
<?php
/**
* Caculate the time elapsed
*
* @param Timestamp $time
* @return String Time Elapsed
* @author Shelley Shyan
* @copyright http://phparch.cn (Professional PHP Architecture)
*/
function time2Units ($time)
{
$year = floor($time / 60 / 60 / 24 / 365);
$time -= $year * 60 * 60 * 24 * 365;
$month = floor($time / 60 / 60 / 24 / 30);
$time -= $month * 60 * 60 * 24 * 30;
$week = floor($time / 60 / 60 / 24 / 7);
$time -= $week * 60 * 60 * 24 * 7;
$day = floor($time / 60 / 60 / 24);
$time -= $day * 60 * 60 * 24;
$hour = floor($time / 60 / 60);
$time -= $hour * 60 * 60;
$minute = floor($time / 60);
$time -= $minute * 60;
$second = $time;
$amount = 0;
$unit = '';
$unitArr = array('year', 'month', 'week', 'day', 'hour', 'minute', 'second');
foreach ( $unitArr as $u )
{
if ( $$u > 0 )
{
$amount = $$u;
$unit = ' ' . $u;
if ( $$u > 1 ) {
$unit .= 's';
}
break;
}
}
return $amount . $unit;
}
$past = 2052345678; // Some timestamp in the past
$now = time(); // Current timestamp
$diff = $now - $past;
echo 'Posted ' . time2Units($diff) . ' ago';
?>
Hope it can give you some help. 