shackwm60 Posted March 3, 2015 Share Posted March 3, 2015 (edited) so i found a function for age some time ago and seems like it works SOME of the time, but ive recently run across some errors; function age($patientsbirthday){ list($day,$month,$year) = explode("/",$patientsbirthday); $year_diff = date("Y") - $year; $month_diff = date("m") - $month; $day_diff = date("d") - $day; if ($day_diff < 0 && $month_diff==0){$year_diff--;} if ($day_diff < 0 && $month_diff < 0){$year_diff--;} return $year_diff; } The persons birthday is input as day, month (DEcember), and year (all varchar) i convert the month to a number using $monthnum = $row['birthmonth']; $monthnum = date_parse($monthnum); and then pass this to the function $patientsbirthday = $row['birthday']. "/" . $monthnum['month'] ."/".$row['birthyear']; heres a couple examples i get returned: echo $patientsbirthday; echo " (" . age($patientsbirthday) . ")"; 10/4/2004 (10) correct 12/12/1991 (23) correct 1/12/2000 (15) incorrect 23/9/1969 (45) correct 3/9/1988 (27) incorrect Anybody see something obvious i am doing wrong here? Edited March 3, 2015 by shackwm60 Quote Link to comment Share on other sites More sharing options...
Barand Posted March 3, 2015 Share Posted March 3, 2015 here's the age function I use function age($dob) /** * $dob - a valid date format (Y-m-d, d-M-Y, m/d/Y, d-m-Y etc) */ { if (!$dob) return "n/a"; $t = strtotime($dob); $age = date('Y') - date('Y', $t); return date('md', $t) > date('md') ? $age-1 : $age; } 1 Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted March 3, 2015 Share Posted March 3, 2015 print date_parse and see if it is listing any errors in the date format print_r(date_parse($monthnum)); Quote Link to comment Share on other sites More sharing options...
maxxd Posted March 3, 2015 Share Posted March 3, 2015 Could you not use DateTime() objects? function getAge($dob='1/1/1970'){ $today = new DateTime(); $dob = new DateTime($dob); $diff = $dob->diff($today); print("You are ".$diff->format('%Y years')." old"); } getAge(); Quote Link to comment Share on other sites More sharing options...
shackwm60 Posted March 3, 2015 Author Share Posted March 3, 2015 (edited) print date_parse and see if it is listing any errors in the date format print_r(date_parse($monthnum)); it returns Warning: date_parse() expects parameter 1 to be string, but $(monthnum) IS a string?? Edited March 3, 2015 by shackwm60 Quote Link to comment Share on other sites More sharing options...
shackwm60 Posted March 3, 2015 Author Share Posted March 3, 2015 (edited) here's the age function I use function age($dob) /** * $dob - a valid date format (Y-m-d, d-M-Y, m/d/Y, d-m-Y etc) */ { if (!$dob) return "n/a"; $t = strtotime($dob); $age = date('Y') - date('Y', $t); return date('md', $t) > date('md') ? $age-1 : $age; } Could you not use DateTime() objects? function getAge($dob='1/1/1970'){ $today = new DateTime(); $dob = new DateTime($dob); $diff = $dob->diff($today); print("You are ".$diff->format('%Y years')." old"); } getAge(); Yea there were many different varieties of Age functions i was looking at but i settled on that one. If i cant get it to work right, i will try the others. Thanks. Edited March 3, 2015 by shackwm60 Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 3, 2015 Share Posted March 3, 2015 (edited) it returns Warning: date_parse() expects parameter 1 to be string, but $(monthnum) IS a string?? Check this var_dump($monthnum); Or, better yet, echo $patientsbirthday to the page. Edited March 3, 2015 by Psycho Quote Link to comment Share on other sites More sharing options...
Barand Posted March 3, 2015 Share Posted March 3, 2015 (edited) Your date formats could present a problem EG echo date('Y-m-d', strtotime('25/12/2014')); // d/m/y format - gives 1970-01-01 X echo date('Y-m-d', strtotime('25-12-2014')); // d-m-y format - gives 2014-12-25 ok echo date('Y-m-d', strtotime('12/25/2014')); // m/d/y format - gives 2014-12-25 ok for UK d/m/y it doesn't like / but d.m.y or d-m-y are ok / is ok for US m/d/y format Better to stick with universal Y-m-d format Edited March 3, 2015 by Barand Quote Link to comment Share on other sites More sharing options...
shackwm60 Posted March 3, 2015 Author Share Posted March 3, 2015 Check this var_dump($monthnum); Or, better yet, echo $patientsbirthday to the page. I DO echo $patientsbirthday to the page in the first post. I dont see any error. And as i indicated, sometime the calculation works, sometimes not. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 3, 2015 Share Posted March 3, 2015 And as i indicated, sometime the calculation works, sometimes not. And as I indicated, sometimes your date format will be valid (even if not interpreted correctly), sometimes it won't Quote Link to comment Share on other sites More sharing options...
shackwm60 Posted March 3, 2015 Author Share Posted March 3, 2015 Your date formats could present a problem EG echo date('Y-m-d', strtotime('25/12/2014')); // d/m/y format - gives 1970-01-01 X echo date('Y-m-d', strtotime('25-12-2014')); // d-m-y format - gives 2014-12-25 ok echo date('Y-m-d', strtotime('12/25/2014')); // m/d/y format - gives 2014-12-25 ok for UK d/m/y it doesn't like / but d.m.y or d-m-y are ok / is ok for US m/d/y format Better to stick with universal Y-m-d format I changed it to the dashes instead of slashes and still the same result.. I will keep plugging. thanks. Quote Link to comment Share on other sites More sharing options...
Solution shackwm60 Posted March 3, 2015 Author Solution Share Posted March 3, 2015 (edited) Ok, well i am using a new function with the date_create function and it works well and is small. function age($birthday) { $ageYears = date_create($birthday)->diff(date_create('today'))->y; return $ageYears; } Thanks for all your assistance. Edited March 3, 2015 by shackwm60 Quote Link to comment 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.