I have a script running on my site. Sort of profile type kinda site. It calculates the age of people by looking at their Birthday, Birthmonth and Birthyear and the current date. However, there is a problem: this way of calculation doesn't display the correct age for all people.
$stampmebaby =getdate(time()) ;
$day= $stampmebaby[mday] ;
$month=$stampmebaby[mon] ;
$year=$stampmebaby[year] ;
$age_year= $year-$row[byear] ;
$age=$age_year ;
if ($month>=$row[bmonth] AND $day>=$row[bday])
$age=$age_year ;
else
$age=$age_year-1 ;
I have tried to explain to myself exactly how the calculation is done in normal, human language... This is what I came up with:
Age = Year - Birthyear
If month is bigger than birthmonth AND day is bigger than birthday
Age = Year - Birthdate
otherwise
Age = Year - Birthdate - 1
Now this would work today (november 21st 2002) for someone who's born on September 17th 1985, because 21 is more than 17. But for someone who's born on January 29th 1986 it won't work, because 29 is more than 21 and then it will jump to the "else $age=$age_year -1" part. This part is necessary sometimes to calculate the correct age.. but not in this case.
I know this might sound complicated.. but please, can anyone give me suggestions on what the proper code would be if I wanted to calculate someone's age in PHP when I was given their birthday, birthmonth and birthyear?