PHP:如何计算几个月的人年龄

Awa*_*wan 8 php function

我搜索过这个,但没有在php中找到完美的功能.我想得到一个PHP函数,只用几个月来计算人的年龄.

例如:

less then one month old.
5 months old.
340 months old.
Run Code Online (Sandbox Code Playgroud)

谢谢

Dan*_*erg 13

使用PHP DateInterval(可从5.3.0获得),这非常简单:

$birthday = new DateTime('1990-10-13');
$diff = $birthday->diff(new DateTime());
$months = $diff->format('%m') + 12 * $diff->format('%y');
Run Code Online (Sandbox Code Playgroud)

现在$months将包含我生活的月数.