|
|
|
|


freebert.com Determine Age Function:
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;
}
?>

Get Support For The Determine Age In Years Given Date Of Birth PHP Function
Get a complete PHP page example using the function, and post any question, comments or other issues in the freebert.com Wiki Support Section for The Determine Age In Years Given Date Of Birth PHP Function

Example Usage For:
Determine Age In Years Given Date Of Birth PHP Function

In any PHP page, be sure to have copied and pasted (or included) the Determine Age In Years Given Date Of Birth PHP Function into the page within PHP tags, of course ('<?php' & '?>'). Then, usage of the function is simple:

<?php
// Example Age Input
$DateOfBirth=19770226;

// Calculate Age Using Function:
$Age= DetermineAgeFromDOB ($DateOfBirth);

// Display Results
echo "A person born on ".$DateOfBirth." is now ".$Age." years old.";
?>




|
|
|
|
- Related Links -

PaymentMerchant.com: eCommerce Web Service Directory