我正在显示 future 3 个月的月份标题,并获取每个月份的第一天和最后一天。
for($i = 1; $i < 4; $i++) { // For each month for 3 months
$monthTitle = date('F Y', strtotime('+'.$i.' month'));
$begin_date = date('Y-m-01', strtotime('+'.$i.' month')); // First day of calendar month in future.
$end_date = date('Y-m-t', strtotime('+'.$i.' month')); // Last day of calendar months in future.
};
十一月29、2015年产出为:
December 2015
2015-12-01
2015-12-31
January 2016
2016-01-01
2016-01-31
February 2016
2016-02-01
2016-02-29
直到昨天,2015 年 11 月 29 日,它一直运行良好,但今天 2015 年 11 月 30 日,它跳过了 2 月。
十一月30、2015年产出为:
December 2015
2015-12-01
2015-12-31
January 2016
2016-01-01
2016-01-31
March 2016
2016-03-01
2016-03-31
我猜是一个错误,但有人知道解决方法吗?
最佳答案
感谢@devlin carnate 为我指明了正确的方向。
for($i = 1; $i < 4; $i++) { # for each month
$tmp = date('Y-m-15'); // Get the middle of the month to avoid PHP date bug.
$begin_date = date('Y-m-01', strtotime($tmp . '+'.$i.' month')); // First day of calendar month in future.
$end_date = date('Y-m-t', strtotime($begin_date)); // Last day of calendar months in future.
$monthTitle = date('F Y', strtotime($begin_date));
};
这似乎工作得很好。
关于PHP 日期函数跳过二月。有人知道解决此日期错误的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34005207/