PHP calculate age

2018-12-31 22:04发布

I'm looking for a way to calculate the age of a person, given their DOB in the format dd/mm/yyyy.

I was using the following function which worked fine for several months until some kind of glitch caused the while loop to never end and grind the entire site to a halt. Since there are almost 100,000 DOBs going through this function several times a day, it's hard to pin down what was causing this.

Does anyone have a more reliable way of calculating the age?

//replace / with - so strtotime works
$dob = strtotime(str_replace("/","-",$birthdayDate));       
$tdate = time();

$age = 0;
while( $tdate > $dob = strtotime('+1 year', $dob))
{
    ++$age;
}
return $age;

EDIT: this function seems to work OK some of the time, but returns "40" for a DOB of 14/09/1986

return floor((time() - strtotime($birthdayDate))/31556926);

标签: php
30条回答
看风景的人
2楼-- · 2018-12-31 22:11

You can use the Carbon library, which is an API extension for DateTime.

You can:

function calculate_age($date) {
    $date = new \Carbon\Carbon($date);
    return (int) $date->diffInYears();
}

or:

$age = (new \Carbon\Carbon($date))->age;
查看更多
余欢
3楼-- · 2018-12-31 22:12

I use the following method to calculate age:

$oDateNow = new DateTime();
$oDateBirth = new DateTime($sDateBirth);

// New interval
$oDateIntervall = $oDateNow->diff($oDateBirth);

// Output
echo $oDateIntervall->y;
查看更多
梦寄多情
4楼-- · 2018-12-31 22:13

I did it like this.

$geboortedatum = 1980-01-30 00:00:00;
echo leeftijd($geboortedatum) 

function leeftijd($geboortedatum) {
    $leeftijd = date('Y')-date('Y', strtotime($geboortedatum));
    if (date('m')<date('m', strtotime($geboortedatum)))
        $leeftijd = $leeftijd-1;
    elseif (date('m')==date('m', strtotime($geboortedatum)))
       if (date('d')<date('d', strtotime($geboortedatum)))
           $leeftijd = $leeftijd-1;
    return $leeftijd;
}
查看更多
临风纵饮
5楼-- · 2018-12-31 22:14

This works fine.

<?php
  //date in mm/dd/yyyy format; or it can be in other formats as well
  $birthDate = "12/17/1983";
  //explode the date to get month, day and year
  $birthDate = explode("/", $birthDate);
  //get age from date or birthdate
  $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
    ? ((date("Y") - $birthDate[2]) - 1)
    : (date("Y") - $birthDate[2]));
  echo "Age is:" . $age;
?>
查看更多
与风俱净
6楼-- · 2018-12-31 22:15

// Age Calculator

function getAge($dob,$condate){ 
            $birthdate = new DateTime(date("Y-m-d",  strtotime(implode('-', array_reverse(explode('/', $dob))))));
            $today= new DateTime(date("Y-m-d",  strtotime(implode('-', array_reverse(explode('/', $condate))))));           
            $age = $birthdate->diff($today)->y;
            return $age;

}

$dob='06/06/1996'; //date of Birth
$condate='07/02/16'; //Certain fix Date of Age 
echo getAge($dob,$condate);
查看更多
皆成旧梦
7楼-- · 2018-12-31 22:15

If you don't need great precision, just the number of years, you could consider using the code below ...

 print floor((time() - strtotime("1971-11-20")) / (60*60*24*365));

You only need to put this into a function and replace the date "1971-11-20" with a variable.

Please note that precision of the code above is not high because of the leap years, i.e. about every 4 years the days are 366 instead of 365. The expression 60*60*24*365 calculates the number of seconds in one year - you can replace it with 31536000.

Another important thing is that because of the use of UNIX Timestamp it has both the Year 1901 and Year 2038 problem which means the the expression above will not work correctly for dates before year 1901 and after year 2038.

If you can live with the limitations mentioned above that code should work for you.

查看更多
登录 后发表回答