为什么
echo date("j/m/Y", strtotime("2015/01/31 00:00 next month"));
给予2015年3月3日而不是2015年2月28日
我要找的只是下个月的今天,如果今天的日期无效,它会给我下个月最后一天。
$thisMonth = "2015-01-31 00:00";
$thisMonthDate = strtotime($thisMonth);
$nextMonthDate = strtotime($thisMonth . ' next month');
if (date('j', $thisMonthDate) !== date('j', $nextMonthDate)) {
$nextMonthDate = strtotime(date('Y-m-d H:i:s', $nextMonthDate) . ' last day of previous month');
}
echo date('Y-m-d H:i:s', $nextMonthDate), PHP_EOL;
PHP在这里所做的是:
首先,它会给你的日期增加一个月,到达"2015/02/31"
然后它意识到这个日期不存在,而且它是在"2015/02/28"之后的3天,用人类的术语翻译,就是"2015/03/03"
在这里查找问题的解决方案,正如@kingkero建议的