Finding Users Age
Moderator: General Moderators
-
php_wiz_kid
- Forum Contributor
- Posts: 181
- Joined: Tue Jun 24, 2003 7:33 pm
Finding Users Age
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.
-
php_wiz_kid
- Forum Contributor
- Posts: 181
- Joined: Tue Jun 24, 2003 7:33 pm
why didnt it help you?
..did u read the page?
this is from the same page, by verszuz at hotmail dot com
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
-
krash_control
- Forum Newbie
- Posts: 14
- Joined: Mon Jan 12, 2004 10:02 am
- Location: United Kingdom
- Contact:
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
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().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.
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
Actually, I found this really good function I found in another past. Here it is:
$dob needs to be in MySQL's date type: YYYY-MM-DD
So
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.
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;}
}
}
?>So
Code: Select all
<?php
$age = findAge('1985-12-14');
echo $age;
?>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.