Hi, I want to calculate age of the person using this code


/*** a date before 1970 ***/
$dob = strtotime("april 20 1961");

/*** another date ***/
$tdate = strtotime("june 2 2009");

/*** show the date ***/
echo getAge( $dob, $tdate );


but it gives error Fatal error: Call to undefined function getage()

9 Years
Discussion Span
Last Post by digital-ether

this code working perfectly.


function yearsOld($birthday)
    if (($birthday = strtotime($birthday)) === false)
        return false;
    for ($i = 0; strtotime("-$i year") > $birthday; ++$i);
    return $i - 1;

 /*** example usage ***/
 $birthday = 'april 20 1961';
 echo yearsOld($birthday).' years old';

It is probably faster to do something like:

function age($birthday) {
  return date("Y", time() - strtotime($birthday)) - 1970;

It first converts the $birthday string to Unix Time. Which is the number of seconds since the epoch (midnight Jan 1 1970 UTC). http://en.wikipedia.org/wiki/Unix_time


Then subtracts the birthday from the current unix time. This gives you the seconds elapsed between their birthday and today.

time() - strtotime($birthday)

Then retrieves the number years that would have elapsed in those seconds, since year 1970.

date("Y", time() - strtotime($birthday))

Then subtracts 1970 from that, to get the number years since birth (year 0).

Edit: limited to dates php can calculate with unix time.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.