Last active
September 7, 2015 16:12
-
-
Save webtrainingwheels/f7d3541c1b79d0a1716e to your computer and use it in GitHub Desktop.
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 how_old_am_i($todaydate, $mybday) { | |
//convert given dates to timestamps | |
$currenttimestamp = strtotime($todaydate); | |
$bdaytimestamp = strtotime($mybday); | |
//get just the years from the timestamps | |
$currentyear = date("Y",$currenttimestamp); | |
$mybd = date ("Y",$bdaytimestamp); | |
//get the current months as numbers (so they can be compared) | |
$currentmonth = date("m", $currenttimestamp); | |
$birthmonth = date("m", $bdaytimestamp); | |
//get the current days as numbers | |
$currentday = date("d",$currenttimestamp ); | |
$birthdate = date("d",$bdaytimestamp ); | |
//if current month is same as birth month AND current day is less than birth day, then age is minus 1 | |
$currentage = ($currentyear - $mybd); | |
if ($currentmonth == $birthmonth && $currentday < $birthdate) { | |
echo 'you are ' . ($currentage - 1) . ' years old'; | |
} | |
//otherwise it's simply current year minus birth year | |
else { | |
echo 'you are ' . ($currentage) . ' years old'; | |
} | |
} | |
how_old_am_i ('1 January 2015', '27 February 1976' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment