Calculate age

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Calculate age

Post 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)?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Calculate age

Post 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));
}
 
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: Calculate age

Post 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).
mdk999
Forum Newbie
Posts: 22
Joined: Fri May 08, 2009 3:21 pm

Re: Calculate age

Post 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
Last edited by Benjamin on Sun May 10, 2009 12:08 am, edited 1 time in total.
Reason: Changed code type from text to php.
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: Calculate age

Post 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).
---------
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Calculate age

Post 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);
}
 
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: Calculate age

Post by JKM »

1988.12.02:

1988
12
02

21

:(
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Calculate age

Post by Benjamin »

Works fine for me.
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: Calculate age

Post by JKM »

Did you test 1988.12.02?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Calculate age

Post by Benjamin »

Yes, I get 20.
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: Calculate age

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Calculate age

Post 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;
}
 
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: Calculate age

Post by JKM »

Nice, thanks! <3
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: Calculate age

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Calculate age

Post by Benjamin »

I don't have time to look at it today, but you should be able to debug it yourself fairly easily.
Post Reply