Calculate Age from Birthday with PHP

So I just whipped up a PHP function that takes a birthdate and outputs an age in years. Here’s the code:

function age_from_birthdate($month, $day, $year) {
return floor((mktime(0, 0, 0) - mktime(0, 0, 0, $month, $day, $year)) / 31556952);
}

It doesn’t specifically take leap years into account, but it compensates by using the average number of days in the Gregorian calendar, 365.2425. It has been tested a little, and seems to be pretty accurate. Of course, it’s not perfect. It doesn’t take the time of day into account either, it just sets the time to midnight. To me, those things are not a huge deal. I am currently using this function on the about page. Please let me know if you find any problems with it. Feel free to use this function in your application. It would be neat to know if you use it, so please let me know! UPDATE: A JavaScript Age From Birthday Calculator.

Leave a Reply