Determine Age In Years Given Date Of Birth PHP Function
This free php function reliably calculates a persons age given their date of birth as input. To use, copy the function below to your PHP page, pass in the date of birth as a parameter, and the function will return as output the age in years in form of an integer. See the full example below the function code:
Function Code For:
The Determine Age In Years Given Date Of Birth PHP Function
Current Version: 1.0
<?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;
}
?>
|
|
|