PHP:如何在几个月+几天内计算人的年龄? [英] PHP: How to calculate person's age in months+days?

查看:195
本文介绍了PHP:如何在几个月+几天内计算人的年龄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用出生日期计算个人年龄(例如:1986-08-23)。

I want to calculate person's age in months plus days using date of birth (example: 1986-08-23).

例如:

0 months and 25 days old.
5 months and 20 days old.
150 months and 4 days old.
285 months and 30 days old.

任何想法?谢谢。

推荐答案

$date = new DateTime('1990-10-13');
$diff = $date->diff(new DateTime());
printf("%d months and %d days old", $diff->y*12 + $diff->m, $diff->d);

请注意, DateTime :: diff() 需要PHP 5.3.0或更高版本。

Note that DateTime::diff() requires PHP 5.3.0 or higher.

这篇关于PHP:如何在几个月+几天内计算人的年龄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆