Created
September 13, 2010 04:36
-
-
Save 0xnbk/576817 to your computer and use it in GitHub Desktop.
Calculate age using date of birth
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
| <?php | |
| function age($date){ | |
| $year_diff = ''; | |
| $time = strtotime($date); | |
| if(FALSE === $time){ | |
| return ''; | |
| } | |
| $date = date('Y-m-d', $time); | |
| list($year,$month,$day) = explode("-",$date); | |
| $year_diff = date("Y") – $year; | |
| $month_diff = date("m") – $month; | |
| $day_diff = date("d") – $day; | |
| if ($day_diff < 0 || $month_diff < 0) $year_diff–; | |
| return $year_diff; | |
| } | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Calculate age using date of birth
Pass a birth date to this function, and it will return the age of the person; very useful when building communities or social media sites.