Skip to content

Instantly share code, notes, and snippets.

@EricYue2012
Created November 14, 2013 03:49
Show Gist options
  • Save EricYue2012/7461035 to your computer and use it in GitHub Desktop.
Save EricYue2012/7461035 to your computer and use it in GitHub Desktop.
Calculate age based on birthday
// version 1:
// param: $birth_date (input string: YYYY-MM-DD)
function birthday ($birth_date){
list($year,$month,$day) = explode("-",$birth_date);
$today = date('d-m-Y');
$a_birthday = explode('-', $birthday);
$a_today = explode('-', $today);
$age = $a_today[2] - $year;
if (($a_today[1] < $month) || ($a_today[1] ==$month && $a_today[0] < $day))
{
$age--;
}
return $age;
}
// version 2
// param $BirthDate -> timestamp
// get age based on birthday
function ageCalculater($BirthDate){
$birthday = date("d-m-Y",$BirthDate);
$today = date('d-m-Y');
$a_birthday = explode('-', $birthday);
$a_today = explode('-', $today);
$age = $a_today[2] - $a_birthday[2];
if (($a_today[1] < $a_birthday[1]) || ($a_today[1] ==$a_birthday[1] && $a_today[0] < $a_birthday[0]))
{
$age--;
}
return $age;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment