Hi, I am trying to work out the age from someones birthday input. How would work out the age from this "1975-04-05" Cheers, Adam
This does work, but not very well some people may know better ways in PHP to do this. Javascript is probally best to use due to the problem you will get for people born before 1970 which is when the unix time() started. /** *Shows age only works with ages after 1970 *because of the timestamp from unix *Format (Day: 00 Month: 00 Year:0000) **/ function age($day, $month, $year) { $dob = mktime(0,0,0,$month, $day, $year); $age = round(time()-$dob) / 31536000); return $age; } PHP:
turn birthdate into a variable in phps date format (lets call taht variable $birthdate for this example), then use following function and if you are on php5 it will return older than 1970 as the date is represented as negative integer and the calculation is still correct - if you are on php4, it wont return for older than 1970 date of birth
I am using php4 so the above will not work, and i need above 1970. I don't want to use a javascript solution either. Does anyone have a PHP solution that will work.
This code should do what you asked for $mDateofBirth = '1935-04-08'; list($iY, $iM, $iD) = split('-',date('Y-m-d', strtotime("now"))); list($iYear, $iMonth, $iDay) = split("-",date('Y-m-d', strtotime($mDateofBirth))); $iAge = ( ($iM < $iMonth) || ( ($iM == $iMonth) && ($iD <= $iDay) ) ) ? ($iY - $iYear - 1 ) : ($iY - $iYear); echo 'You are ' . $iAge . ' years old'; PHP: Same thing wrapped as a function function GetBirthDate ($p_mDateOfBirth) { list($iY, $iM, $iD) = split('-',date('Y-m-d', strtotime('now'))); list($iYear, $iMonth, $iDay) = split('-',date('Y-m-d', strtotime($p_mDateOfBirth))); $iAge = ( ($iM < $iMonth) || ( ($iM == $iMonth) && ($iD <= $iDay) ) ) ? ($iY - $iYear - 1) : ($iY - $iYear); return $iAge; } // Usage example echo 'You are ' . GetBirthDate ('1975-04-05') . ' years old'; PHP: