karatekid36 Posted July 9, 2007 Share Posted July 9, 2007 I have some birthdates in a table in the YYYY-MM-DD format and I was curious what the best way is to calculate a person's age so that it could in turn be displayed in a web page? Link to comment https://forums.phpfreaks.com/topic/59130-calculating-age/ Share on other sites More sharing options...
paul2463 Posted July 9, 2007 Share Posted July 9, 2007 $now = date("Y"); //year of now $query = "SELECT DATE_FORMAT(date_column,'%Y') AS birthdate FROM table"; $result = mysql_query($query) or die ("cannot get dates" . mysql_error()); $row = mysql_fetch_assoc($result); $bdate = $row['birthdate']; //year the person was born $age = $now - $bdate; //now - year of birth will work for part of the year but needs some maths modfications to make it work for the rest of the year because if i was born in september 1966 I am 40 but because the above will use 2007 and 1966 I will be 41 Link to comment https://forums.phpfreaks.com/topic/59130-calculating-age/#findComment-293697 Share on other sites More sharing options...
Wildbug Posted July 9, 2007 Share Posted July 9, 2007 SELECT YEAR(NOW()) - YEAR(birthdate) - (MONTH(birthdate) < MONTH(NOW()) OR DAYOFMONTH(birthdate) < DAYOFMONTH(NOW())) AS Age FROM tbl; Link to comment https://forums.phpfreaks.com/topic/59130-calculating-age/#findComment-293795 Share on other sites More sharing options...
fenway Posted July 10, 2007 Share Posted July 10, 2007 There's a TO_DAYS() function that you can easily use to get days old. Link to comment https://forums.phpfreaks.com/topic/59130-calculating-age/#findComment-294643 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.