Rottingham Posted February 3, 2007 Share Posted February 3, 2007 Hello All! I know you can use an mySQL query to convert a date value to an Age, but I'm trying to figure out how to do it in PHP. If I have a date, 1978-02-03, what would I use to convert that into an age? It should = 29. Thanks for any advice! It is 2am and I'm sleepy! Link to comment https://forums.phpfreaks.com/topic/36896-help-converting-date-to-age/ Share on other sites More sharing options...
hvle Posted February 3, 2007 Share Posted February 3, 2007 SELECT (YEAR(CURDATE())-YEAR('1978-02-03'))-(RIGHT(CURDATE(),5)<RIGHT('1978-02-03',5)) as age; if you have a column birthdate in table t, use like this: SELECT (YEAR(CURDATE())-YEAR(birthdate))-(RIGHT(CURDATE(),5)<RIGHT(birthdate,5)) from t as age; Link to comment https://forums.phpfreaks.com/topic/36896-help-converting-date-to-age/#findComment-176012 Share on other sites More sharing options...
Rottingham Posted February 3, 2007 Author Share Posted February 3, 2007 Thank you for the help on this. I'm sorry I didn't communicate very well! Like I said, it was 2am. I am looking to see how it is done outside of the query. I.e I have already pulled the data, and at some point later I want to take $DOB and do something like get_age($DOB); Link to comment https://forums.phpfreaks.com/topic/36896-help-converting-date-to-age/#findComment-176191 Share on other sites More sharing options...
hvle Posted February 3, 2007 Share Posted February 3, 2007 Actually, I am the one who doesn't read well. it is similar: <?php $birth = '1975-02-05'; $now = date('Y-m-d'); // get today's year and date $age = substr($now, 0, 4) - substr($birth, 0, 4); if (substr($birth,-5) > substr($now,-5)) // if birthdate later than today's date $age -= 1; echo $age; ?> Link to comment https://forums.phpfreaks.com/topic/36896-help-converting-date-to-age/#findComment-176423 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.