evil_stevo Posted December 15, 2010 Share Posted December 15, 2010 //calculate age $birthdate = "1978-04-26"; //birth date... actually being obtained from a database $today = date("Y-m-d H:i:s"); // The exact date $age = date_diff($str_birthday, $today); echo $age; I'd like a simple code to echo the age of someone with the mysql database information that's in their record. This doesn't work. I have no idea why. Nothing seems to work that I've found on the net. Please help. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/221776-finding-age-from-birthdate/ Share on other sites More sharing options...
salathe Posted December 15, 2010 Share Posted December 15, 2010 What do you mean "doesn't work"? Are any errors displayed, or logged? What version of PHP are you using? Does date_diff() work for you in other scripts? Finally, and importantly, have you read the manual page for date_diff()? Quote Link to comment https://forums.phpfreaks.com/topic/221776-finding-age-from-birthdate/#findComment-1147765 Share on other sites More sharing options...
litebearer Posted December 15, 2010 Share Posted December 15, 2010 Perhaps... <?php $dob = "1978-04-26"; $ageTime = strtotime($dob); // Get the person's birthday timestamp $t = time(); // Store current time for consistency $age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;$year = 60 * 60 * 24 * 365; $ageYears = $age / $year; ?> Quote Link to comment https://forums.phpfreaks.com/topic/221776-finding-age-from-birthdate/#findComment-1147771 Share on other sites More sharing options...
evil_stevo Posted December 15, 2010 Author Share Posted December 15, 2010 That worked great litebearer!!! I did a little modification for my own and got rid of the decimal and this came out... $birthdate = "1985-07-15"; //calculate age $ageTime = strtotime($birthdate); // Birthday Timestamp $t = time(); // Current Time $age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;$year = 60 * 60 * 24 * 365; $ageYears = $age / $year; $age = number_format($ageYears,0); //Delete Decimals echo $age; Let me know what you think... THANKS AGAIN! IT WAS JUST THE TICKET!!!! :) Quote Link to comment https://forums.phpfreaks.com/topic/221776-finding-age-from-birthdate/#findComment-1147783 Share on other sites More sharing options...
litebearer Posted December 15, 2010 Share Posted December 15, 2010 Yep - using floor() would also work Quote Link to comment https://forums.phpfreaks.com/topic/221776-finding-age-from-birthdate/#findComment-1147784 Share on other sites More sharing options...
PFMaBiSmAd Posted December 15, 2010 Share Posted December 15, 2010 Or you can just do this in your query when you retrieve the data. See the first example at this link - http://dev.mysql.com/doc/refman/5.0/en/date-calculations.html Quote Link to comment https://forums.phpfreaks.com/topic/221776-finding-age-from-birthdate/#findComment-1147795 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.