Calculate age from DOB
Posted: Tue Aug 18, 2009 8:24 pm
Hi everyone, I have done a search on this topic and the closest I came to generating my date of birth on a webpage was out by a whole 9years lol (I'm 30 not 39)
I have user data stored in a database which includes $birthdate in a YYYY/MM/DD format, using Juma929's code as below:
As i say this produced 39years old.
I'd just like some clues as to where its gone wrong or a prefered snippest of code to use.
Ive called the $birthdate info in my code
Any suggestions welcome
I have user data stored in a database which includes $birthdate in a YYYY/MM/DD format, using Juma929's code as below:
Code: Select all
# $dob = strtotime($birthdate);
# $date_today = strtotime(date("Y/m/d"));
#
# $agex = $date_today - $dob;
#
# $years = floor($agex / 31556926);
# $remainder = $agex % 31556926;
# $months = floor($remainder / 2592000);
#
# echo $years . ' ' . $months;I'd just like some clues as to where its gone wrong or a prefered snippest of code to use.
Ive called the $birthdate info in my code
Code: Select all
$birthdate = mysql_query("SELECT birthdate FROM users WHERE id='$id'");
$birthdatearray = mysql_fetch_assoc($birthdate);
$dob = strtotime($birthdate);
$date_today = strtotime(date("Y/m/d"));
$agex = $date_today - $dob;
$years = floor($agex / 31556926);
$remainder = $agex % 31556926;
$months = floor($remainder / 2592000);
echo $years . ' ' . $months;