PHP strtotime +1 个月增加一个月

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14584115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:34:34  来源:igfitidea点击:

PHP strtotime +1 month adding an extra month

phpdatestrtotime

提问by Jason

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

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

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

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.

今天是 2013 年 1 月,所以我希望回到 2013-02,但我得到的是 2013-03。我不明白为什么它跳到三月。

回答by SDC

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.

它跳到三月,因为今天是 1 月 29 日,再加上一个月就是 2 月 29 日,而这个日期不存在,所以它移到下一个有效日期。

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.

这也将发生在许多月份的 31 日,但在 1 月至 2 月的情况下显然更明显,因为 2 月较短。

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.

出于同样的原因,如果你想总是得到下个月的最后一天,你应该从你想要的那个月的第一天开始计算,然后减去一天。

回答by One Man Crew

This should be

这应该是

$endOfCycle=date('Y-m-d', strtotime("+30 days"));

strtotime

strtotime

expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

期望得到一个包含美国英语日期格式的字符串,并将尝试将该格式解析为 Unix 时间戳(自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数),相对于 now 中给出的时间戳,或如果没有提供现在,则为当前时间。

while

尽管

date

date

Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.

返回根据给定格式字符串格式化的字符串,使用给定的整数时间戳或当前时间(如果没有给定时间戳)。

See the manual pages for:

请参阅手册页了解:

回答by Salman A

You can use this code to get the next month:

您可以使用此代码获取下个月:

$ts = mktime(0, 0, 0, date("n") + 1, 1);
echo date("Y-m-d H:i:s", $ts);
echo date("n", $ts);

Assuming today is 2013-01-31 01:23:45the above will return:

假设今天是2013-01-31 01:23:45上面将返回:

2013-02-01 00:00:00
2

回答by Trudbert

Maybe because its 2013-01-29 so +1 month would be 2013-02-29 which doesn't exist so it would be 2013-03-01

也许是因为它的 2013-01-29 所以 +1 个月将是 2013-02-29 不存在所以它会是 2013-03-01

You could try

你可以试试

date('m/d/y h:i a',(strtotime('next month',strtotime(date('m/01/y')))));

from the comments on http://php.net/manual/en/function.strtotime.php

来自http://php.net/manual/en/function.strtotime.php上的评论

回答by Blacksonic

today is 29th of January, +1 month means 29th of Fabruary, but because February consists of 28 days this year, it overlaps to the next day which is March 1st

今天是 1 月 29 日,+1 月表示 29 日,但由于今年 2 月有 28 天,因此与第二天即 3 月 1 日重叠

instead try

而是尝试

strtotime('next month')

回答by sharkyenergy

 $endOfCycle = date("Y-m", mktime(0, 0, 0, date("m", time())+1 , 15, date("m", time())));

回答by sharkyenergy

try this:

尝试这个:

$endOfCycle = date("Y-m", time()+2592000);

this adds 30 days, not exactly a month tough.

这增加了 30 天,而不是一个月的艰难。