Time Calculator In PHP

18th June 2008 - 1 minute 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,

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:

 [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.