PHP DateInterval not returning last day of the month

折月煮酒 提交于 2019-12-05 12:33:47

The problem is that the end date/time itself is never included in the period -- that is, the DatePeriod object represents date/times in the [$startDate, $endDate) range¹.

You have to add one second at least to the end date to always get the expected results. But let's not be stingy and add a whole minute:

$days = new DatePeriod(
    new DateTime("first $day of $m $y"),
    DateInterval::createFromDateString('next ' . $day),
    new DateTime("last day of $m $y 00:01")
);

¹ unless you use the DatePeriod::EXCLUDE_START_DATE option, in which case the start date itself is excluded as well

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