Finding Users 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
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Finding Users Age

Post 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.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post by php_wiz_kid »

That didn't help me any. Can anybody else help?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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)));
?>
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post 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.
krash_control
Forum Newbie
Posts: 14
Joined: Mon Jan 12, 2004 10:02 am
Location: United Kingdom
Contact:

Post 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.
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

maybe this script will help

http://www.lodilinks.com/thankyou/agecheck.zip

Mark
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post 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.
Post Reply