Time Calculator In PHP

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:

Array
(
 [years] => 23.721673198884
 [months] => 721.53422646605
 [weeks] => 1236.9158167989
 [days] => 8658.4107175926
 [hours] => 207801.85722222
 [minutes] => 12468111.433333
 [seconds] => 748086686
)

Any function like this can be tested by putting in the current time, if you get zero across the board then the function works.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
7 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.