While most people are perfectly fine just seeing the date of an item, it's also nice to be able to show your visitors the age of this item as well. By 'age', I mean how long ago an item was published, or modified, or added, etc. The below function will help you easily display the item's age. All you have to do it pass it the item's date.
I've been seeing this feature on a lot of sites recently, the most popular being Digg. With Digg, for example, it shows you how long ago an item was submitted, made popular, commented on, etc. Looking around the Internet I couldn't really find well documented tutorials on how to do this, so I decided to create my own function and share it with you here.
Now, there is one main thing to consider here. It is the format of the item's date that you're passing into the function. In this example the item's date format is in a UNIX TIMESTAMP format (the number of seconds from the Unix Epoch - Jan 1, 1970). If your item's date is not in this format, then you can simply use the strtotime() function to convert it to a UNIX TIMESTAMP.
Below is the function with included comments to help you understand exactly what's going on (or click here to see the source):
function getItemAge($date) {
// First we subtract the current date and time
// from the item's date and time
$time_diff = time() - $date;
// Now we check difference and convert it
// into secs, mins, hrs, days, years
// Less than a min ago
if ($time_diff < 60) {
$age = $time_diff . ' secs ago';
// Between 1 and 2 mins
} elseif ($time_diff >= 60 && $time_diff < 120) {
$mins = 1;
$secs = $time_diff - ($mins * 60);
$age = $mins . ' min ' . $secs . ' secs ago';
// Between 2 and 59 mins
} elseif ($time_diff >= 120 && $time_diff < 3600) {
$mins = floor($time_diff / 60);
$secs = $time_diff - ($mins * 60);
$age = $mins . ' mins ' . $secs . ' secs ago';
// Between 1 and 2 hours
} elseif ($time_diff >= 3600 && $time_diff < 7200) {
$hrs = 1;
$mins = floor(($time_diff - ($hrs * 3600)) / 60);
$secs = $time_diff - ($hrs * 3600) - ($mins * 60);
$age = $hrs . ' hr ' . $mins . ' mins ' . $secs . ' secs ago';
// Between 2 and 23 hours
} elseif ($time_diff >= 7200 && $time_diff < 86400) {
$hrs = floor($time_diff / 3600);
$mins = floor(($time_diff - ($hrs * 3600)) / 60);
$secs = $time_diff - ($hrs * 3600) - ($mins * 60);
$age = $hrs . ' hrs ' . $mins . ' mins ' . $secs . ' secs ago';
// Between 1 and 2 days
} elseif ($time_diff >= 86400 && $time_diff < 172800) {
$days = 1;
$hrs = floor(($time_diff - ($days * 86400)) / 3600);
$mins = floor(($time_diff - ($days * 86400) - ($hrs * 3600)) / 60);
$secs = $time_diff - ($days * 86400) - ($hrs * 3600) - ($mins * 60);
$age = $days . ' day ' . $hrs . ' hrs ' . $mins . ' mins ' . $secs . ' secs ago';
// Between 2 and 364 days
} elseif ($time_diff >= 172800 && $time_diff < 31536000) {
$days = floor($time_diff / 86400);
$hrs = floor(($time_diff - ($days * 86400)) / 3600);
$mins = floor(($time_diff - ($days * 86400) - ($hrs * 3600)) / 60);
$secs = $time_diff - ($days * 86400) - ($hrs * 3600) - ($mins * 60);
$age = $days . ' days ' . $hrs . ' hrs ' . $mins . ' mins ' . $secs . ' secs ago';
// Between 1 and 2 years
} elseif ($time_diff >= 31536000 && $time_diff < 63072000) {
$yrs = 1;
$days = floor(($time_diff - ($yrs * 31536000)) / 86400);
$hrs = floor(($time_diff - ($yrs * 31536000) - ($days * 86400)) / 3600);
$mins = floor(($time_diff - ($yrs * 31536000) - ($days * 86400) - ($hrs * 3600)) / 60);
$secs = $time_diff - ($yrs * 31536000) - ($days * 86400) - ($hrs * 3600) - ($mins * 60);
$age = $yrs . ' yr ' . $days . ' days ' . $hrs . ' hrs ' . $mins . ' mins ' . $secs . ' secs ago';
// 2 years or greater
} elseif ($time_diff >= 63072000) {
$yrs = floor($time_diff / 31536000);
$days = floor(($time_diff - ($yrs * 31536000)) / 86400);
$hrs = floor(($time_diff - ($yrs * 31536000) - ($days * 86400)) / 3600);
$mins = floor(($time_diff - ($yrs * 31536000) - ($days * 86400) - ($hrs * 3600)) / 60);
$secs = $time_diff - ($yrs * 31536000) - ($days * 86400) - ($hrs * 3600) - ($mins * 60);
$age = $yrs . ' yrs ' . $days . ' days ' . $hrs . ' hrs ' . $mins . ' mins ' . $secs . ' secs ago';
}
return $age;
}
In the above function, we pass one argument to it - the item's date. Again, this item's date is in a UNIX TIMESTAMP format. If you pass any other type of date format to it, the function will not work.
To use this function, simply do the following:
$item_age = getItemAge(1192932420);
echo $item_age;
And that's it! I'm a novice PHP coder at best, so while the above function does work, there may be other ways, or more streamlined ways of doing this. To view the complete function click here >> getItemAge Function.

February 20, 2008 11:28 am
2423 Views

Autosaving with jQuery and TinyMCE
Be the first to comment by using the form below.