supanoob Posted March 17, 2009 Share Posted March 17, 2009 ok so i have a database field for my date of birth that is formatted like so DD/MM/YYYY is there anyway to use that information to determin how old a person is? Quote Link to comment https://forums.phpfreaks.com/topic/149864-calculating-age/ Share on other sites More sharing options...
Zhadus Posted March 17, 2009 Share Posted March 17, 2009 <?php $birthday = split('/', $birth); $years = date('Y') - $birthday[2]; if (birthday[1] > date('m')) { $years++; } elseif ((birthday[1] == date('n')) && (birthday[0] >= date('j'))) { $years++; } ?> Untested, and might be an easier way, but that should work. Quote Link to comment https://forums.phpfreaks.com/topic/149864-calculating-age/#findComment-787018 Share on other sites More sharing options...
PFMaBiSmAd Posted March 17, 2009 Share Posted March 17, 2009 After you get your DD/MM/YYYY format into a usable mysql DATE format, you can calculate the age using the query at this link - http://dev.mysql.com/doc/refman/5.0/en/date-calculations.html Quote Link to comment https://forums.phpfreaks.com/topic/149864-calculating-age/#findComment-787022 Share on other sites More sharing options...
Maq Posted March 17, 2009 Share Posted March 17, 2009 The best way is to perform the calculation straight in the query, as PFMaBiSmAd displayed. If you are going to do it from PHP (which is pointless if you're grabbing the dates from the DB) you can use something much simpler: floor((time() - strtotime($birthday))/31556926); Quote Link to comment https://forums.phpfreaks.com/topic/149864-calculating-age/#findComment-787026 Share on other sites More sharing options...
supanoob Posted March 17, 2009 Author Share Posted March 17, 2009 The best way is to perform the calculation straight in the query, as PFMaBiSmAd displayed. If you are going to do it from PHP (which is pointless if you're grabbing the dates from the DB) you can use something much simpler: floor((time() - strtotime($birthday))/31556926); thats the only one that actually worked for me and it was a year out i kept getting You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '5) with the query method :S Quote Link to comment https://forums.phpfreaks.com/topic/149864-calculating-age/#findComment-787058 Share on other sites More sharing options...
PFMaBiSmAd Posted March 17, 2009 Share Posted March 17, 2009 The subtraction method does not work because the definition of the age of anything is the difference in years, subtract one if the birthday has not yet occurred in the current year. The is what the equation at the link I posted does and it can be ported in php code if you need. Cannot help you with your sql syntax error unless you post the actual code that generated the error. Quote Link to comment https://forums.phpfreaks.com/topic/149864-calculating-age/#findComment-787069 Share on other sites More sharing options...
Maq Posted March 17, 2009 Share Posted March 17, 2009 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '5) with the query method :S Query? Quote Link to comment https://forums.phpfreaks.com/topic/149864-calculating-age/#findComment-787071 Share on other sites More sharing options...
supanoob Posted March 17, 2009 Author Share Posted March 17, 2009 $query="select user_dob, CURDATE(), (YEAR(CURDATE(),5)<RIGHT(user_dob)) AS age from user_accounts where user_id='$user_id'"; $result=mysql_query($query); if (!$result) { die(mysql_error()); } $num_rows=mysql_num_rows($result); $row=mysql_fetch_array($result); $age = ($row['age']); Quote Link to comment https://forums.phpfreaks.com/topic/149864-calculating-age/#findComment-787084 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.