Skip to content

Instantly share code, notes, and snippets.

@antz29
Created July 26, 2010 09:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antz29/490367 to your computer and use it in GitHub Desktop.
Save antz29/490367 to your computer and use it in GitHub Desktop.
Simple function using PHP DateTime object to calculate an exact age from a DOB.
<?php
function calculateAge($dob,$in_format="d/m/Y",$out_format="%y years, %m months,$
{
$dob = DateTime::createFromFormat($in_format, $dob);
$int = $dob->diff(new DateTime());
return $int->format($out_format);
}
<?php
require "calculate_age.php";
echo calculateAge("08/06/1995");
// 15 years, 1 months, 19 days
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment