Page 1 of 1

Calculate age

Posted: Sat May 09, 2009 9:48 am
by JKM
Hi there!

How do I calculate age for a user when I've got f.i. 02-21-87 in the mysql db (TEXT)?

Re: Calculate age

Posted: Sat May 09, 2009 10:08 am
by Benjamin
Here's a function I wrote that will tell you the age of something in years based on a date.

Code: Select all

 
function get_age($year, $month, $day) {
    return ((date("d") - (int)$day) < 0 || (date("m") - (int)$month) < 0) ? (date("Y") - (int)$year) : (date("Y") - ((int)$year - 1));
}
 

Re: Calculate age

Posted: Sat May 09, 2009 8:59 pm
by JKM

Code: Select all

<?php
function doAge($year, $month, $day) {
return ((date("d") - (int)$day) < 0 || (date("m") - (int)$month) < 0) ? (date("Y") - (int)$year) : (date("Y") - ((int)$year - 1));
}
?>
<p><?php echo doAge($fetch['born_date']); ?></p>
=

Code: Select all

Warning: Missing argument 2 for doAge(), called in /home/age.php on line 112 and defined in /home/age.php on line 108
 
Warning: Missing argument 3 for doAge(), called in /home/age.php on line 112 and defined in /home/age.php on line 108
I added '1987-02-21' to the row, but it returned '23' (22 is correct). I also tried 1988-12-02, which returned 22 (20 is correct).

Re: Calculate age

Posted: Sat May 09, 2009 10:07 pm
by mdk999
You have to break up the result you get back from mysql so ..

Code: Select all

$dob = explode('/',$fetch);//fetch = 09/20/1987
This gives you

Code: Select all

echo $dob[0]; //09
echo $dob[1]; //20
echo $dob[2]; //197
You then plug those into the function call

Code: Select all

doAge($dob[0], $dob[1], $dob[2]);
Hope that helps

Re: Calculate age

Posted: Sun May 10, 2009 8:03 am
by JKM

Code: Select all

<?php
function doAge($year, $month, $day) {
    return ((date("d") - (int)$day) < 0 || (date("m") - (int)$month) < 0) ? (date("Y") - (int)$year) : (date("Y") - ((int)$year - 1));
}
$dob = explode('.',$fetch['born_date']);
echo $dob[0].'<br />';
echo $dob[1].'<br />';
echo $dob[2].'<br /><br />';
echo doAge($dob[0], $dob[1], $dob[2]);
?>
1988.12.02 returned:
1988
12
02

21 (20 is correct).
----
1987.02.21 returned:
1987
02
21

22 (22 is correct).
----
1987.06.15 returned:
1987
06
15

22 (21 is correct).
---------

Re: Calculate age

Posted: Sun May 10, 2009 12:06 pm
by Benjamin
:(

Weird.. try this.

Code: Select all

 
function doAge($year, $month, $day) {
    return ((date('m') - (int)$month <= 0) && (date("d") - (int)$day > 0)) ? date('Y') - (int)($year) - 1 : date('Y') - (int)($year);
}
 

Re: Calculate age

Posted: Sun May 10, 2009 2:35 pm
by JKM
1988.12.02:

1988
12
02

21

:(

Re: Calculate age

Posted: Sun May 10, 2009 3:15 pm
by Benjamin
Works fine for me.

Re: Calculate age

Posted: Sun May 10, 2009 3:41 pm
by JKM
Did you test 1988.12.02?

Re: Calculate age

Posted: Sun May 10, 2009 4:21 pm
by Benjamin
Yes, I get 20.

Re: Calculate age

Posted: Sun May 10, 2009 4:55 pm
by JKM
Hmm, yeah, it worked for me too with 1988, 12, 02. But if you replace 02 with 10 (1988, 12, 10), it says 21. :p

Re: Calculate age

Posted: Sun May 10, 2009 9:33 pm
by Benjamin
I'm sorry, I was in a hurry and didn't check it as well as I should have. Here are two versions that test ok.

Code: Select all

 
function doAge($year, $month, $day) {
  # if this month minus month of birth is greater than or equal to 0
  if (date('m') - (int)$month >= 0) {
      # if this date minus the date of birth is greater than or equal to 0
      if (date("d") - (int)$day >= 0) {
          return date('Y') - (int)($year);
      }
  }
  return date('Y') - (int)($year) - 1;
}
 
function doAge2($year, $month, $day) {
    return ((date('m') - (int)$month >= 0) && (date("d") - (int)$day >= 0)) ? date('Y') - (int)($year) : date('Y') - (int)($year) - 1;
}
 

Re: Calculate age

Posted: Wed May 13, 2009 7:12 am
by JKM
Nice, thanks! <3

Re: Calculate age

Posted: Mon Jun 15, 2009 11:49 am
by JKM
I'm using the first one, and 1988.05.23 returns 20 - but it should return 21. 1988.12.23 returns 20 - as it should.

Re: Calculate age

Posted: Mon Jun 15, 2009 12:31 pm
by Benjamin
I don't have time to look at it today, but you should be able to debug it yourself fairly easily.