PHP Strtotime -1个月 -2个月
昨天工作正常,没有更改代码。
echo date("M", strtotime("-3 month", time()) );
echo date("M", strtotime("-2 month", time()) );
echo date("M", strtotime("-1 month", time()) );
echo date("M", time());
它昨天产生的输出正如你所期望的——即四月、五月、六月、七月
今天它呼应了五月五月七月七月
有什么想法吗?
提前致谢。
This was working fine yesterday with no changes to the code.
echo date("M", strtotime("-3 month", time()) );
echo date("M", strtotime("-2 month", time()) );
echo date("M", strtotime("-1 month", time()) );
echo date("M", time());
The output it was producing yesterday was as you would expect- i.e. Apr, May, Jun, Jul
Today it echoes May May Jul Jul
Any ideas?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它可能与错误 #44073 有关,
您可以尝试使用以下方法:
(在
strtotime
的评论部分找到解决方案;直接链接)输出:
有点“作弊”日期格式和月份名称等等......
It might be related to bug #44073
You could try with something like this :
(Solution found in the comments section of
strtotime
; direct link)And the output :
Kind of "cheating" with the date format and month's name and all that...
戈登正确地识别了这个问题,但我想提供另一个有用但不那么技术性的解决方案。 只需在时间中使用“第一天”或“最后一天”即可。 例如,以下示例在每月 31 日解决了该问题:
Gorden correctly identified the issue, but I wanted to give another solution that was helpful and not as technical. Just use "first day of" or "last day of" in your strtotime. Ex, these following examples overcome the issue on a 31st of a month:
试试这个而不是
strtotime
:这个想法是获取当前月份数字 (
date("n")
),从中减去您想要的月份数(这里< code>-3),加上 12,然后对 12 求模。Try this instead of
strtotime
:The idea is to take the current month number (
date("n")
), substract the number of months from it you want (here-3
), add 12 to it and then get modulo 12.这是已知且预期的行为。 原因是月是一个没有固定长度的相对日期。 来自 http://www. gnu.org/software/tar/manual/html_node/Relative-items-in-date-strings.html#SEC120
因此(强调我的)
上述内容可以用 PHP 表示为
另请参阅下面的注释 http://bugs .php.net/bug.php?id=22486
This is known and expected behavior. The reason for this is that a month is a relative date with no fixed length. From http://www.gnu.org/software/tar/manual/html_node/Relative-items-in-date-strings.html#SEC120
Consequently (emphasis mine)
The above could be expressed in PHP as
Also see the commments below http://bugs.php.net/bug.php?id=22486
从 PHP 5.3 开始(参考:https://bugs.php.net/bug.php?id =44073)及更高版本,您可以执行以下操作:
“
+1 个月的第一天
”或“下个月的第一天
”甚至“最后一天下个月的某一天
”示例:
将生成:
2015-11-01
这不会以 +30 天的时间为基础计算天数。From PHP 5.3 (Ref: https://bugs.php.net/bug.php?id=44073) and higher you can do:
"
first day of +1 month
" or "first day of next month
" or even "last day of next month
"Example:
Will produce:
2015-11-01
This does not calculate days on +30 days time basis.一种简单的方法可能是像这样写
echo date("M", strtotime("今年 +3 个月"));
或
echo date("M", strtotime("本月-3个月"));
取决于你用它做什么..
a simple way could be to write it like this
echo date("M", strtotime("this year +3 months"));
or
echo date("M", strtotime("this month -3 months"));
depends what your using it for..