PHP strtotime +1个月增加一个月 [英] PHP strtotime +1 month adding an extra month

查看:1190
本文介绍了PHP strtotime +1个月增加一个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的变量,今天增加一个月:

I have a simple variable that adds one month to today:

$endOfCycle = date("Y-m", strtotime("+1 month"));

今天是2013年1月,所以我希望能回到2013-02,但是我得到了2013 -03代替。我不知道为什么跳到三月。

Today is January 2013, so I would expect to get back 2013-02 but I'm getting 2013-03 instead. I can't figure out why it's jumping to March.

推荐答案

跳到三月是因为今天是1月29日,月份将于二月二十九日(不存在),所以转移到下一个有效日期。

It's jumping to March because today is 29th Jan, and adding a month gives 29th Feb, which doesn't exist, so it's moving to the next valid date.

这也将发生在很多个月的31日,但是在2月份的2月份更为明显的情况下,显然更为明显。

This will happen on the 31st of a lot of months as well, but is obviously more noticable in the case of January to Feburary because Feb is shorter.

如果您对月份不感兴趣,只想要给下一个月,您应该将输入日期指定为当前月份的第一个。如果您添加一个月,这将永远给您正确的答案。

If you're not interested in the day of month and just want it to give the next month, you should specify the input date as the first of the current month. This will always give you the correct answer if you add a month.

同样的原因,如果你想永远得到下个月的最后一天,你应该首先计算一个月后的第一个月,然后减去一天。

For the same reason, if you want to always get the last day of the next month, you should start by calculating the first of the month after the one you want, and subtracting a day.

这篇关于PHP strtotime +1个月增加一个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆