Calculating Age in PHP
I was working on Frisbase this morning and encountered the need to calculate a user's age from his or her birthday, as stored in the MySQL date format. Initial googling presented no simple solution, no cute, mathematical one-liner. Unwilling to throw in a big custom function for what should be a simple calculation, I came up with:
$age = floor((time() - strtotime($birthdate))/(60*60*24*365));
For the uninitiated, the functions time and strtotime return Unix timestamp, or the number of seconds since January 1st, 1970. So, I calculate the difference in those timestamps and then divide by the number of seconds in a year. The function floor drops the fractional remainder and I have a nice integer age in years.
This solution seems to work fine and I'm curious as to what input will break it. I would have assumed to find a solution similarly simple on the web. I don't think I have much of a CS audience, but do any of you see possible errors in this approach?
From Tiger Media
Commented November 9th, 2006 4:39 pm
I knew someone would have a nice and simple age function kicking around so I wouldn't have to code one.
Thanks for posting it!
Jay
Tiger Media
From drewd
Commented December 6th, 2006 10:35 am
Calculating Age in PHP, Update...
Frisbase had me turning 25 three days early. Oops! The corrected code is:
$age = floor((time() - strtotime($birthdate))/(60*60*24*365.2425));
I could use 365.24219 but 365.2425 only gives one day of error every 4,000 years. I can live with that.
......
From Bjorn
Commented February 4th, 2007 1:34 am
What about leap years?
From Calculating Age in PHP, Update at drewd
Commented October 17th, 2007 3:32 pm
[...] see my original post, Calculating Age in PHP, for a full explanation of the [...]
From Harrie Verveer
Commented March 6th, 2008 8:36 am
Works fine as long as you aren't born before 1970. People born before 1970 are screwed if you calculate somebodys age this way
From Harrie Verveer
Commented March 6th, 2008 8:47 am
How about this?
$age = date('Y') - $year;
if (date('m') < $month || (date('m') == $month && date('d') < $day))
$age--;
Works for people older than 38 as well
From drew
Commented March 6th, 2008 11:41 am
You're incorrect, Harrie, regarding the 1970 cutoff. Sort of. Let me explain.
On Unix systems, the past and future limits are 1901-12-13 and 2038-01-18. These are the negative and positive values of the maximum number of seconds representable by a 32-bit integer. See Wikipedia for more.
So, for dates before 1970, the timestamp is negative. Today's timestamp minus a negative timestamp is a positive sum. The wonders of arithmetic...
On Windows, before PHP version 5.1, the early limit on representable dates was in fact 1970-01-01. See the PHP Date manual.
So, if you were on Windows before 5.1, or really old PHP on Unix, you would be right. On a current system, my code still wins.
From Harrie Verveer
Commented March 7th, 2008 1:37 am
Omg, I tried it on one of our linux servers (PHP 5.2.5 cli) and... it works! Amazing
I'm into the PHP business for quite some time so I guess I have developed some kind of shield against using timestamps for birthdates (as soon as I see 'something with a birthdate' involving timestamps the alarm bells start to ring - because they caused a lot of trouble in the past).
So thanks for opening my eyes
From JD
Commented March 28th, 2012 9:56 am
If the earlier DAY of compare is today, this will not show proper representation. and that 4000 yr issue is just plain bad. Consider refactoring to somthing more like `intval(substr(date('Ymd') - date('Ymd', strtotime($birthdate)), 0, -4))` as that is a better representation of how we humans calculate birthdays