Time Calculator In PHP
Published by philipnorton42 on Wed, 06/18/2008 - 09:54Use the following function to work out how long it has been since an event in years, months, weeks, days, hours, minutes and seconds.
1 2 3 4 5 6 7 8 9 10 11 | 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.
1 2 | // someone's birthday echo '<pre>'.print_r(getAge(1984,10,4),true).' |
Which would output the following:
1 2 3 4 5 6 7 8 9 10 | 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.
Category:
Add new comment