PHP :需要根据日历结束日期查找两个日期之间的所有日期


PHP : Need to find all dates between two dates as per calendar end date

我需要php解决方案。 我将在其中指定两个日期,并需要根据日历结束日期获取所有日期。

例如:

开始日期 : 14-三月-2016结束日期:2016年5月27日。

要创建发票,我需要找到每个日历结束日期的所有日期。在这里,您可以看到 2016 年 5 月 27 日不会获得 5 月的最后一个日历日期,因为指定的结束日期是 5 月 27 日。所以那个月我应该得到最后日期 27 个月。

简而言之,输出应该像31-三月-201630-四月-20162016 年 5 月 27 日。

你能帮我如何在PHP中做到这一点吗?

t 返回月份中的天数

$date = "2009-11-23";
echo date("Y-m-t", strtotime($date));

你需要以不同的方式使用 date() 函数来获得结果。请查看下面的代码,这可能会对您有所帮助,

$fromDate = '14-Mar-2016';
$toDate = '27-Jun-2016';
echo date("t-M-Y", strtotime($fromDate)).' '; // show first month last date
$month_diff = (int)abs((strtotime($toDate) - strtotime($fromDate))/(60*60*24*30));
for($i=1;$i<$month_diff;$i++)
{
    echo date('t-M-Y', strtotime("+$i month", strtotime($fromDate))).' '; // in between month end
}
if(date("t",strtotime($toDate)) == date("d",strtotime($toDate)))
{
    echo date("t-M-Y", strtotime($toDate)).' '; // last month end
}
else
{
    echo date("d-M-Y", strtotime($toDate)).' '; // last date
}