Jump to content

Calculate Age


shackwm60

Recommended Posts

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

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

 

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.