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

<?php

/*** 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()

this code working perfectly.

<?php


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

strtotime($birthday)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.