我怎样才能以年、月和日为单位计算年龄

  • 本文关键字:为单位 计算 php
  • 更新时间 :
  • 英文 :


这是我的代码:

<?php 
$userDob = $result['DoB'];
//Create a DateTime object using the user's date of birth.
$dob = new DateTime($userDob);
//We need to compare the user's date of birth with today's date.
$now = new DateTime();
//Calculate the time difference between the two dates.
$difference = $now->diff($dob);
//Get the difference in years, as we are looking for the user's age.
$age = $difference->y;
//Print it out.
echo $age;
?>

我想输出 50 年、6 个月、20 天之类的东西,但我不知道如何准确表达,因为这种方法只输出年数。

是的,php 内置了一些东西:)

$age_check = "$day-$month-$year";
$today = date("Y-m-d");
$calculate = date_diff(date_create($age_check),date_create($today));
$age = $calculate->format('%y');

最新更新