【发布时间】:2019-05-16 06:53:13
【问题描述】:
我有一个表格,用户可以在其中输入患者的年龄(年、月和日)。这将根据患者的出生日期将数据保存在数据库中。问题是我正在使用 strtotime() 函数来获取患者的出生日期,但是当我想显示从生日列中检索日期的年龄(年、月、日)时,有时它显示 1 或 2 天 (+-) 结果。我正在使用 date_diff() 函数来检索年龄(年、月、日)。我的问题有什么解决方案吗?
$today = date("Y-m-d");
$dateOfBirth = strtotime("$today -2 years -2 months -20 days");
$birthday = date("Y-m-d", $dateOfBirth);
//and then tried to retrieve the age from that date which give me wrong answer
$diff = date_diff(date_create($birthday), date_create($today));
echo $diff->y; // showing 2
echo $diff->m; // showing 2
echo $diff->d; // showing 22 (it should be 20)
【问题讨论】: