如何使用PHP计算出生日期的年龄? [重复]

This question already has an answer here:

My Code :

<?php
  echo date('d/ m /Y', strtotime($reg_bday));
?>

How to calculate age with this function?

</div>

You can use Carbon PHP class https://carbon.nesbot.com/docs/

Carbon::createFromDate(1991, 7, 19)->diff(Carbon::now())->format('%y years, %m months and %d days')

Output like "23 years, 6 months and 26 days"

Or native PHP based on another answer https://stackoverflow.com/a/3776843/5441049

<?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;
?> 

Short Answer :

function calc_age($date)
{
   return((int)date_diff(date_create($date),date_create('today'))->y);
}

Using :

echo calc_age("1967/03/12");

Related Links :