Created
November 14, 2013 03:49
-
-
Save EricYue2012/7461035 to your computer and use it in GitHub Desktop.
Calculate age based on birthday
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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