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 Years, 6 Months, 20 Days 但我不知道如何准确地说出来,因为这种方法只输出年数。
解决方案
是的,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');
推荐阅读
- amazon-web-services - 如何访问 cloudformation 模板中的当前用户?
- java - java fx 应用程序中未显示的按钮
- r - 如何强制 geom_point 在 R 中显示比例大小?
- c# - 我需要手动处理 TraceListener 吗?
- playframework - Scala.JS(和 Play)中的会话
- android - 从 genymotion 设备卸载 Superuser 应用程序
- pandas - 计算熊猫中连续值组的最大运行长度
- c++ - 将 unique_ptr 分配给原始指针
- tensorflow - tf.train.Saver 实例不关闭检查点文件
- go - golng 获取 url 和 resp.Body.Close()