Work out age from birthday date?

Discussion in 'PHP' started by adamjblakey, Jan 17, 2008.

  1. #1
    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
     
    adamjblakey, Jan 17, 2008 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    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:
     
    HuggyStudios, Jan 17, 2008 IP
  3. logondotinfo

    logondotinfo Peon

    Messages:
    314
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    logondotinfo, Jan 17, 2008 IP
  4. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #4
    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.
     
    adamjblakey, Jan 17, 2008 IP
  5. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #5
    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:
    :)
     
    wing, Jan 17, 2008 IP