shackwm60 Posted March 3, 2015 Share Posted March 3, 2015 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? Link to comment https://forums.phpfreaks.com/topic/295056-calculate-age/ 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; } Link to comment https://forums.phpfreaks.com/topic/295056-calculate-age/#findComment-1507404 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)); Link to comment https://forums.phpfreaks.com/topic/295056-calculate-age/#findComment-1507406 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(); Link to comment https://forums.phpfreaks.com/topic/295056-calculate-age/#findComment-1507408 Share on other sites More sharing options...
shackwm60 Posted March 3, 2015 Author 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)); it returns Warning: date_parse() expects parameter 1 to be string, but $(monthnum) IS a string?? Link to comment https://forums.phpfreaks.com/topic/295056-calculate-age/#findComment-1507409 Share on other sites More sharing options...
shackwm60 Posted March 3, 2015 Author 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; } 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. Link to comment https://forums.phpfreaks.com/topic/295056-calculate-age/#findComment-1507410 Share on other sites More sharing options...
Psycho Posted March 3, 2015 Share Posted March 3, 2015 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. Link to comment https://forums.phpfreaks.com/topic/295056-calculate-age/#findComment-1507411 Share on other sites More sharing options...
Barand Posted March 3, 2015 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 Link to comment https://forums.phpfreaks.com/topic/295056-calculate-age/#findComment-1507414 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. Link to comment https://forums.phpfreaks.com/topic/295056-calculate-age/#findComment-1507416 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 Link to comment https://forums.phpfreaks.com/topic/295056-calculate-age/#findComment-1507418 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. Link to comment https://forums.phpfreaks.com/topic/295056-calculate-age/#findComment-1507423 Share on other sites More sharing options...
shackwm60 Posted March 3, 2015 Author Share Posted March 3, 2015 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. Link to comment https://forums.phpfreaks.com/topic/295056-calculate-age/#findComment-1507426 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.