stevenjweir Posted September 27, 2011 Share Posted September 27, 2011 Hi all, I'm having a bit of trouble a script running on a site where it converts a date of birth in a database shown like this '30/04/1993' to an actual age, for instance 18 in this case. Only the script I'm using below shows this age as 17, not 18 as it should be. <?php $birthday = $row_getdets['dob']; function birthday ($birthday){ list($day,$month,$year) = explode("/",$birthday); $day_diff = date("d") - $day; $month_diff = date("m") - $month; $year_diff = date("Y") - $year; if ($day_diff < 0 || $month_diff < 0) $year_diff--; return $year_diff; } ?> So i've tried to remedy this myself with the following: <?php $birthday = $row_getdets['dob']; function birthday ($birthday){ list($year,$month,$day) = explode("/",$birthday); $year_diff = date("Y") - $year; $month_diff = date("m") - $month; $day_diff = date("d") - $day; if ($month_diff < 0) $year_diff--; else if (($month_diff==0) && ($day_diff < 0)) $year_diff--; return $year_diff; } ?> ..but I'm having a syntax error (unexpected T_LINE), most probably down to my novice ability, I bet I've missed something simple. I'm still learning guys and I'd really appreciate any help at all. Quote Link to comment https://forums.phpfreaks.com/topic/247940-convert-a-date-of-birth-to-age/ Share on other sites More sharing options...
codefossa Posted September 27, 2011 Share Posted September 27, 2011 Works for me. <?php $birthdate = '1992/04/19'; // $birthdate = '1992/10/23'; // $birthdate = '1992/09/26'; // $birthdate = '1992/09/27'; // $birthdate = '1992/09/28'; function getAge($dob) { list($year, $month, $day) = explode('/', $dob); $years = date('Y') - $year; $months = date('m') - $month; $days = date('d') - $day; if ($months < 0 || ($months == 0 && $days < 0)) $years--; return $years; } echo getAge($birthdate); ?> Quote Link to comment https://forums.phpfreaks.com/topic/247940-convert-a-date-of-birth-to-age/#findComment-1273199 Share on other sites More sharing options...
stevenjweir Posted September 27, 2011 Author Share Posted September 27, 2011 Thank you!!, also thank you for laying it out in an easy way for me to understand. Quote Link to comment https://forums.phpfreaks.com/topic/247940-convert-a-date-of-birth-to-age/#findComment-1273205 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.