4

我正在尝试为先前输入的日期范围制作一个带有多个选项卡的 ajax 日历。但例如:

我想得到下个月,它打印三月而不是二月

$start= "2013-01-31";
$current =  date('n', strtotime("+1 month",$start)) //prints 3

我认为这是因为 2014 年 2 月是 28 并且从开始月份开始添加 +31 之类的基数,但为什么呢?

4

1 回答 1

8

您正在尝试将一个月添加到 date 2013-01-31。它应该给出 2013 年 2 月 31 日,但由于日期不存在,它会移动到下一个有效月份(即 3 月)。

您可以使用以下解决方法:

$current = date('n', strtotime("first day of next month",strtotime($start)));

使用DateTime类:

$date = new DateTime('2013-01-31');
$date->modify('first day of next month');
echo $date->format('n');

这将正确输出2

演示!

于 2013-11-01T13:20:34.810 回答