如何从给定日期获取日期和月份数量 [英] How to get days and months number from a given days
本文介绍了如何从给定日期获取日期和月份数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要编写一个函数来打印两个日期之间剩余的天数。我希望它能告诉我们剩下的几个月和几天。例如:
45天= 1个月,15天
65天= 2个月,5天
10天= 10天
所以我试过了:
< ;?
$ days = 50;
if($ days>31){
$ time = $ days / 30;
}
echo $ time; // 1.67 month
?>
根据上面的代码。我预计结果如下:
1个月,20天
请问你们建议。
/ p>
$天= 50;
if($ days> 31){
$ month = floor($ days / 30); //返回最低整数
$ days = $ days%30; //计算剩余天数
}
echo $ month。 =>。 $天; //输出'1 => 20`
I'm going to write a function to print a number of days left between two dates. I would like it to tell the months and days left. For example:
45 days = 1month, 15 days
65 days = 2months, 5 days
10 days = 10 days
So I tried:
<?
$days=50;
if($days>"31"){
$time=$days/30;
}
echo $time;//1.67 month
?>
According to the code above. I expect the result to be like:
1 month, 20 days
Could you guys please suggest.
解决方案
Try:
$days = 50;
if ($days > 31){
$month = floor($days/30); // return lowest whole integer
$days = $days % 30; // calculate left days
}
echo $month . " => " . $days; // output `1 => 20`
这篇关于如何从给定日期获取日期和月份数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文