Jump to content

Calculate Age


shackwm60
Go to solution Solved by 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?

Edited by shackwm60
Link to comment
Share on other sites

 

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 by shackwm60
Link to comment
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.

 

Edited by shackwm60
Link to comment
Share on other sites

 

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 by Psycho
Link to comment
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

Edited by Barand
Link to comment
Share on other sites

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
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
Share on other sites

  • Solution

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 by shackwm60
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.