I'm looking for a way to calculate the age of a person, given their DOB in the format dd/mm/yyyy.
I was using the following function which worked fine for several months until some kind of glitch caused the while loop to never end and grind the entire site to a halt. Since there are almost 100,000 DOBs going through this function several times a day, it's hard to pin down what was causing this.
Does anyone have a more reliable way of calculating the age?
//replace / with - so strtotime works
$dob = strtotime(str_replace("/","-",$birthdayDate));
$tdate = time();
$age = 0;
while( $tdate > $dob = strtotime('+1 year', $dob))
{
++$age;
}
return $age;
EDIT: this function seems to work OK some of the time, but returns "40" for a DOB of 14/09/1986
return floor((time() - strtotime($birthdayDate))/31556926);
I use Date/Time for this:
Due to leap year, it is not wise just to subtract one date from another and floor it to number of years. To calculate the age like the humans, you will need something like this:
this is my function to calculating DOB with the specific return of age by year, month, and day
this how to use it
This function will return the age in years. Input value is a date formated (YYYY-MM-DD) day of birth string eg: 2000-01-01
It works with day - precision
Cheers, nira
The following works great for me and seems to be a lot simpler than the examples that have already been given.
I've tested and actively use this code, it might seem a little cumbersome but it is very simple to use and edit and is quite accurate.