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.