18th June 2008 - 2 minutes read time
Use the following function to work out how long it has been since an event in years, months, weeks, days, hours, minutes and seconds.
function getAge($year,$month,$day,$hour=0,$minute=0,$second=0){
$age = mktime($hour,$minute,$second,$month,$day,$year);
$age = time()-$age;
return array('years'=>$age/60/60/24/365,
'months'=>$age/60/60/24/12,
'weeks'=>$age/60/60/24/7,
'days'=>$age/60/60/24,
'hours'=>$age/60/60,
'minutes'=>$age/60,
'seconds'=>$age);
}
The practical use of this function is that you can work out how old someone is from their birthday. Here is an example of the function in use.
// someone's birthday
echo '<pre>'.print_r(getAge(1984,10,4),true).'</pre>';
Which would output the following: