[英]PHP: How to calculate person age in months
我搜索过这个,但没有在php中找到完美的功能。 我想得到一个PHP函数,只用几个月来计算人的年龄。
例如:
less then one month old.
5 months old.
340 months old.
谢谢
使用PHP的DateInterval
(可从5.3.0获得),这非常简单:
$birthday = new DateTime('1990-10-13');
$diff = $birthday->diff(new DateTime());
$months = $diff->format('%m') + 12 * $diff->format('%y');
现在$months
将包含我所居住的月数。
$birthday = new DateTime("June 21st 1986");
$diff = $birthday->diff(new DateTime());
$months = ($diff->y * 12) + $diff->m;
var_dump($months);
沿着这些方向的东西?
如果你想要几年到几个月,那么这个人将他们的年龄输入为$years
$month = $years*12;
如果你想要几个月到几年,那么这个人将他们的年龄输入为$months
$year = $months/12
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.