Calculate age
Moderator: General Moderators
Calculate age
Hi there!
How do I calculate age for a user when I've got f.i. 02-21-87 in the mysql db (TEXT)?
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
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
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 108Re: Calculate age
You have to break up the result you get back from mysql so ..
This gives you
You then plug those into the function call
Hope that helps
Code: Select all
$dob = explode('/',$fetch);//fetch = 09/20/1987Code: Select all
echo $dob[0]; //09
echo $dob[1]; //20
echo $dob[2]; //197Code: Select all
doAge($dob[0], $dob[1], $dob[2]);
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.
Reason: Changed code type from text to php.
Re: Calculate age
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
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
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
1988.12.02:
1988
12
02
21

1988
12
02
21
Re: Calculate age
Works fine for me.
Re: Calculate age
Did you test 1988.12.02?
Re: Calculate age
Yes, I get 20.
Re: Calculate age
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
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
Nice, thanks! <3
Re: Calculate age
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
I don't have time to look at it today, but you should be able to debug it yourself fairly easily.