How to get days and months number from a given days

这一生的挚爱 提交于 2019-12-13 06:46:13

问题


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.


回答1:


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`



回答2:


Get the month number for both months and subtract. Add in calculation for year change

Get day of months for both dates. If date2 > date1, subtract and you have the number of days, else remove 1 from month count and sumteact date



来源:https://stackoverflow.com/questions/14941957/how-to-get-days-and-months-number-from-a-given-days

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!