PHP将月份中的日期添加到日期中的月份中的第31天

Ben*_*enB 1 php datetime date strtotime

我使用的代码在所有日期(2015年5月31日除外)都能正常运行。该代码带给我下个月的第一天。即使每个月的日期是31,它也适用于每个日期。

$time = strtotime('2015-07-31');
$final = date("Y-m-1", strtotime("+1 month", $time));
echo $final;
Run Code Online (Sandbox Code Playgroud)

输出将是-> 2015-08-1。

由于某些原因,日期为2015-05-31,因此返回2015-07-1,而不是2015-06-01

$time = strtotime('2015-05-31');
$final = date("Y-m-1", strtotime("+1 month", $time));
echo $final;
Run Code Online (Sandbox Code Playgroud)

其可能是因为6-2014有30天,而8-2014有31天,所以+1个月加了30天而不是一个“月”。

在每个日期下个月的第一天,我如何正确获取?

谢谢。

小智 5

我认为这应该有效-

$time = strtotime("2015-05-31");
echo $final = date("Y-m-d", strtotime("first day of next month", $time));
Run Code Online (Sandbox Code Playgroud)