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:24
 $date = new DateTime($bithdayDate);
 $now = new DateTime();
 $interval = $now->diff($date);
 return $interval->y;
查看更多
初与友歌
3楼-- · 2018-12-31 22:25

I use Date/Time for this:

$age = date_diff(date_create($bdate), date_create('now'))->y;
查看更多
高级女魔头
4楼-- · 2018-12-31 22:25

Due to leap year, it is not wise just to subtract one date from another and floor it to number of years. To calculate the age like the humans, you will need something like this:

$birthday_date = '1977-04-01';
$age = date('Y') - substr($birthday_date, 0, 4);
if (strtotime(date('Y-m-d')) - strtotime(date('Y') . substr($birthday_date, 4, 6)) < 0)
{
    $age--;
}
查看更多
无与为乐者.
5楼-- · 2018-12-31 22:29

this is my function to calculating DOB with the specific return of age by year, month, and day

function ageDOB($y=2014,$m=12,$d=31){ /* $y = year, $m = month, $d = day */
date_default_timezone_set("Asia/Jakarta"); /* can change with others time zone */

$ageY = date("Y")-intval($y);
$ageM = date("n")-intval($m);
$ageD = date("j")-intval($d);

if ($ageD < 0){
    $ageD = $ageD += date("t");
    $ageM--;
    }
if ($ageM < 0){
    $ageM+=12;
    $ageY--;
    }
if ($ageY < 0){ $ageD = $ageM = $ageY = -1; }
return array( 'y'=>$ageY, 'm'=>$ageM, 'd'=>$ageD );
}

this how to use it

$age = ageDOB(1984,5,8); /* with my local time is 2014-07-01 */
echo sprintf("age = %d years %d months %d days",$age['y'],$age['m'],$age['d']); /* output -> age = 29 year 1 month 24 day */
查看更多
若你有天会懂
6楼-- · 2018-12-31 22:29

This function will return the age in years. Input value is a date formated (YYYY-MM-DD) day of birth string eg: 2000-01-01

It works with day - precision

function getAge($dob) {
    //calculate years of age (input string: YYYY-MM-DD)
    list($year, $month, $day) = explode("-", $dob);

    $year_diff  = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff   = date("d") - $day;

    // if we are any month before the birthdate: year - 1 
    // OR if we are in the month of birth but on a day 
    // before the actual birth day: year - 1
    if ( ($month_diff < 0 ) || ($month_diff === 0 && $day_diff < 0))
        $year_diff--;   

    return $year_diff;
}

Cheers, nira

查看更多
临风纵饮
7楼-- · 2018-12-31 22:29

The following works great for me and seems to be a lot simpler than the examples that have already been given.

$dob_date = "01";
$dob_month = "01";
$dob_year = "1970";
$year = gmdate("Y");
$month = gmdate("m");
$day = gmdate("d");
$age = $year-$dob_year; // $age calculates the user's age determined by only the year
if($month < $dob_month) { // this checks if the current month is before the user's month of birth
  $age = $age-1;
} else if($month == $dob_month && $day >= $dob_date) { // this checks if the current month is the same as the user's month of birth and then checks if it is the user's birthday or if it is after it
  $age = $age;
} else if($month == $dob_month && $day < $dob_date) { //this checks if the current month is the user's month of birth and checks if it before the user's birthday
  $age = $age-1;
} else {
  $age = $age;
}

I've tested and actively use this code, it might seem a little cumbersome but it is very simple to use and edit and is quite accurate.

查看更多
登录 后发表回答