Forum Moderators: coopster
Can anyone see what is wrong?
<?php
function DetermineAgeFromDOB ($YYYYMMDD_In)
{
// Parse Birthday Input Into Local Variables
// Assumes Input In Form: YYYYMMDD
$yIn=substr($YYYYMMDD_In, 0, 4);
$mIn=substr($YYYYMMDD_In, 4, 2);
$dIn=substr($YYYYMMDD_In, 6, 2);
// Calculate Differences Between Birthday And Now
// By Subtracting Birthday From Current Date
$ddiff = date("d") - $dIn;
$mdiff = date("m") - $mIn;
$ydiff = date("Y") - $yIn;
// Check If Birthday Month Has Been Reached
if ($mdiff < 0)
{
// Birthday Month Not Reached
// Subtract 1 Year From Age
$ydiff--;
} elseif ($mdiff==0)
{
// Birthday Month Currently
// Check If BirthdayDay Passed
if ($ddiff < 0)
{
//Birthday Not Reached
// Subtract 1 Year From Age
$ydiff--;
}
}
return $ydiff;
}
?>
preg_match('#(\d{4})(\d{2})(\d{2})#',$YYYYMMDD_In,$parts);
$birth = strtotime("{$parts[1]}-{$parts[2]}-{$parts[3]}");
$seconds = time() - $birth;
$age = (int) ($seconds / 31557600.0);
The first line extracts the date parts from the format you want to use. The second turns it into a timestamp. Then we subtract it off of today's timestamp and divide by the number of seconds in a year (including 1/4 day per year to account for leap years).
<?php require("age.php"); ?>
<?php
// Example Age Input
$DateOfBirth=$row_member_profile['handler_dob'];
// Calculate Age Using Function:
$Age= DetermineAgeFromDOB ($DateOfBirth);
// Display Results
echo $Age ;
?>
what would I use for yours?
[edited by: Darylt at 8:17 pm (utc) on July 31, 2008]