I need to to display guardian related form fields if the user is younger than 18 years old. Now, if I ave user date of birth in the following format 0000-00-00 How do I calculate the difference? Probably basic but I've reached my coding block ...
[RESOLVED] Age verification
if you're wanting to handle it in PHP, i'd use a function like this:
function calculateAge($bday) {
$birth = strtotime($bday);
$ageStamp = time() - $birth;
$year = 60 * 60 * 24 * 365; // not accounting for leap year!!!
return floor($ageStamp / $year);
}
echo "I am " . calculateAge("1980-02-15") . " years old!";
you'll start getting errors with people born before Jan 1, 1970, so you may want to set a year check first.
good luck
That's great, thanks. I can leave with leap year exception.
I just attempted running the function but keep getting strange result:
This code:
echo calculateAge($bday)." - ".$bday;
Prints this result:
36 - 1942-01-03
No matter what the DOB is it just keeps getting 36...
OK, I think the problem is somewhere around $birth
Here's what I put in code:
echo calculateAge($bday)." = ". $birth." = ".$ageStamp." = ".$year." = ".$bday;
And here's what I get on the page:
36 = = = 1942 = 1942-10-31
a couple things:
1) what format are you presenting "$bday" as when you call the function?
2) you cannot echo the function variables ($birth, $ageStamp, etc) from outside the function.
here is the entire script i have, and it seems to run just fine.
<?php
function calculateAge($bday) {
$birth = strtotime($bday);
$ageStamp = time() - $birth;
$year = 60 * 60 * 24 * 365; // not accounting for leap year!!!
return floor($ageStamp / $year);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
echo "I am " . calculateAge("1980-02-15") . " years old!";
?>
</body>
</html>
if you're going to use this throughout your pages, just use an include file or something similar to include the function at the top, then call it as you populate your page:
$sql = mysql_query("SELECT * FROM users");
while ($user = mysql_fetch_array($sql)) {
echo "$row[username] = " . calculateAge($row['bday']) . "<br />\n";
}
I am thoroughly confused...
I put your code as is and it works great, but when I plug in my date it gives me "36" as age...
Hrere's the code I put:
echo "I am " . calculateAge($usr->act_dob) . " years old! Date test: ".$usr->act_dob;
And here what I get:
I am 36 years old! Date test: 1960-12-31
zzz wrote:I am thoroughly confused...
I put your code as is and it works great, but when I plug in my date it gives me "36" as age...
Hrere's the code I put:
echo "I am " . calculateAge($usr->act_dob) . " years old! Date test: ".$usr->act_dob;
And here what I get:
well, that's what i was referring to above. since getting the UNIX timestamp with PHP functions such as strtotime() won't work on dates before Jan 1, 1970, any older dates will throw all sorts of errant data at you. basically, the script i gave would only work for the situation you described above. for instance, you need to set a fixed year that sets the base age you KNOW is valid. then, you can check by individual birthdays beyond that. try something like this:
function validateUserAge($bday, $min = 21) {
list($year, $month, $day) = explode('-', $bday);
$minYear = date('Y') - $min;
if ($year >= $minYear) {
$age = calculateAge($bday);
if ($age < $min)
return false;
}
return true;
}
function calculateAge($bday) {
$birth = strtotime($bday);
$ageStamp = time() - $birth;
$year = 60 * 60 * 24 * 365; // not accounting for leap year!!!
return floor($ageStamp / $year);
}
//--------------------------------------------------
// now, let's put those functions to use
$minAge = 18;
$bdays = array("1992-04-15", "1988-10-28", "1960-06-12");
foreach ($bdays as $bday) {
if (validateUserAge($bday, $minAge))
echo "user is old enough<br />\n";
else
echo "user is too young!<br />\n";
}
basically, this lets you submit a minimum age for the function to return TRUE or FALSE that the user is at least that old.
hope this helps some.
Ahh... I think this is going to do the trick! This is not for an adult site, but does need to collect additional data if age is < 18. Lemme try this.
zzz wrote:Ahh... I think this is going to do the trick! This is not for an adult site, but does need to collect additional data if age is < 18. Lemme try this.
i didn't figure it was for an adult site, that's why i left the age open ended ;-)... that way, whatever age you pass in on the function call, you'll get an appropriate response.
It works great!