[英]PHP: How to calculate person age in months
I have searched for this but did not find a perfect function in php. 我搜索过这个,但没有在php中找到完美的功能。 I want to get a php function that calculate person age only in months.
我想得到一个PHP函数,只用几个月来计算人的年龄。
For example: 例如:
less then one month old.
5 months old.
340 months old.
Thanks 谢谢
Using PHP's DateInterval
(available from 5.3.0), that's pretty easy: 使用PHP的
DateInterval
(可从5.3.0获得),这非常简单:
$birthday = new DateTime('1990-10-13');
$diff = $birthday->diff(new DateTime());
$months = $diff->format('%m') + 12 * $diff->format('%y');
Now $months
will contain the number of months I've lived. 现在
$months
将包含我所居住的月数。
$birthday = new DateTime("June 21st 1986");
$diff = $birthday->diff(new DateTime());
$months = ($diff->y * 12) + $diff->m;
var_dump($months);
Something along these lines? 沿着这些方向的东西?
If you want years to months, where the person inputs their ages as $years
如果你想要几年到几个月,那么这个人将他们的年龄输入为
$years
$month = $years*12;
If you want months to years, where the person inputs their age as $months
如果你想要几个月到几年,那么这个人将他们的年龄输入为
$months
$year = $months/12
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.