Page 1 of 1
Finding Users Age
Posted: Mon Jan 12, 2004 9:52 am
by php_wiz_kid
I'm trying to figure out a way to make sure the user is above a certain age and I was wondering there was some equation to figure this out with the current date and the users birthdate. Thanks.
Posted: Mon Jan 12, 2004 9:54 am
by qads
Posted: Mon Jan 12, 2004 10:08 am
by php_wiz_kid
That didn't help me any. Can anybody else help?
Posted: Mon Jan 12, 2004 10:10 am
by qads
why didnt it help you?

..did u read the page?
this is from the same page, by verszuz at hotmail dot com
Code: Select all
<?php
$day=27;//the day of birth variable
$month=10;//the month of birth variable
$year=1977;// the year of birth variable
$now=mktime();
echo $now."<br>";
$timebirth=mktime(0,0,0,$month,$day,$year);
echo $timebirth."<br>";
$agetime=$now-$timebirth;
echo ((strftime("%Y",$agetime))-(strftime("%Y",0)));
?>
Posted: Mon Jan 12, 2004 10:17 am
by php_wiz_kid
That doesn't work. It's saying that I'm a year older than I am, and I could get that by simply subtracting 2004 by my birthyear.
Posted: Mon Jan 12, 2004 10:23 am
by krash_control
Pardon my ignorance but isn't that how age is calculated? i.e. Current year minus birth year. If you want to get picky I guess you could always add a few if statements to check if the birth month has passed in which case the above code is correct, and if it hasn't then just subtract 1 year. If you want to be even more picky, you can check if the current month is equal to the birth month, then apply the above principal of subtracting 1, according to the day.
Posted: Mon Jan 12, 2004 10:27 am
by php_wiz_kid
Well, I was thinking that there would be a way to do it without if statements, but I guess I could do it that way. Thanks for the help guys.
Posted: Mon Jan 12, 2004 10:33 am
by JayBird
Posted: Mon Jan 12, 2004 2:55 pm
by McGruff
php_wiz_kid wrote:Well, I was thinking that there would be a way to do it without if statements, but I guess I could do it that way. Thanks for the help guys.
Once you've got age in seconds (using unix timestamps) you could divide by the average number of seconds in a year and use floor().
Average seconds in a year = 60x60x24x365 + (60x60x24/4).
If this isn't sufficiently accurate (it might be out by a day or so), you will have to check if the birthday in the current year has elapsed.
Posted: Wed Jan 14, 2004 10:09 am
by php_wiz_kid
Actually, I found this really good function I found in another past. Here it is:
Code: Select all
<?php
function findAge ($dob){
$year=substr($dob,0,4); $month=substr($dob,5,2); $day=substr($dob,8,2); # get year, month, day from dob
$today = localtime();
$age = ($today[5]+1900) - $year; // this can be a year to old!
if ($month < ($today[4]+1)) {return $age;}
elseif ($month > ($today[4]+1)) {$age--; return $age;} // over by a year
else{ // it's the month the user was born. we need to check the day
if ($day > $today[3]) {$age--; return $age;} // over by a year
else {return $age;}
}
}
?>
$dob needs to be in MySQL's date type: YYYY-MM-DD
So
Code: Select all
<?php
$age = findAge('1985-12-14');
echo $age;
?>
would equal 18.
NOTE: Make sure that the date format is 4-2-2. If it is a single digit month make sure to put a 0 infront of it, example:
1985-01-01.
If you don't the year will be 1 off.