-
-
Save grim-reapper/ada216f46f11b20db5a27ce02902bb90 to your computer and use it in GitHub Desktop.
PHP function to calculate age
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 calculate_age($birthday) | |
| { | |
| $today = new DateTime(); | |
| $diff = $today->diff(new DateTime($birthday)); | |
| if ($diff->y) | |
| { | |
| return ($diff->y == 1) ? $diff->y . ' year' : $diff->y . ' years'; | |
| } | |
| elseif ($diff->m) | |
| { | |
| return ($diff->m == 1) ? $diff->m . ' month' : $diff->m . ' months'; | |
| } | |
| else | |
| { | |
| return ($diff->d == 1) ? $diff->d . ' day' : $diff->d . ' days'; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment