Page 1 of 1

computing age from mySQL date

Posted: Wed Dec 08, 2010 11:44 am
by hlamster
I have a date retrieved from mySQL and am trying to compute a persons age.

$p1DOBphp = strtotime($p1DOB);
$p1Age = date() - $p1DOBphp;
$p1AgeYr = DATE('Y',$p1Age);

where $p1DOB is the Date Of Birth in a SQL date format (i.e. yyyy-mm-dd)

But I get weird results. What am I doing wrong.

Thanks/Hal

Re: computing age from mySQL date

Posted: Wed Dec 08, 2010 12:25 pm
by AbraCadaver

Code: Select all

$p1AgeYr = ( time() - $p1DOBphp ) / 31556926;
//or
$p1AgeYr = floor( ( time() - $p1DOBphp ) / 31556926 );

Re: computing age from mySQL date

Posted: Wed Dec 08, 2010 12:41 pm
by hlamster
Works like a charm. Thanks.

Re: computing age from mySQL date

Posted: Wed Dec 08, 2010 12:50 pm
by AbraCadaver
hlamster wrote:Works like a charm. Thanks.
Since I forgot to explain:

You are taking the birthdate as number of seconds since 1970/01/01 and subtracting the current date as number of seconds since 1970/01/01, resulting in the difference in seconds. So divide that by the number of seconds in 1 year to get the number of years.