PHP Strtotime -1month -2month
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1211824/
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
PHP Strtotime -1month -2month
提问by Pascal MARTIN
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.
提前致谢。
回答by Pascal MARTIN
It might be related to bug #44073
可能与错误#44073 有关
You could try with something like this :
你可以尝试这样的事情:
echo date("M", strtotime("-3 month", strtotime(date("F") . "1")) ) . "\n";
echo date("M", strtotime("-2 month", strtotime(date("F") . "1")) ) . "\n";
echo date("M", strtotime("-1 month", strtotime(date("F") . "1")) ) . "\n";
echo date("M", time()) . "\n";
(Solution found in the comments section of strtotime; direct link)
And the output :
和输出:
Apr
May
Jun
Jul
Kind of "cheating" with the date format and month's name and all that...
日期格式和月份名称等等的“欺骗”类型......
回答by SeanDowney
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:
Gorden 正确地识别了这个问题,但我想提供另一个有用的而不是技术性的解决方案。只需在 strtotime 中使用“第一天”或“最后一天”即可。例如,以下示例解决了每月 31 日的问题:
// Today is May 31st
//All the following return 2012-04-30
echo date('Y-m-d', strtotime("last day of -1 month"));
echo date('Y-m-d', strtotime("last day of last month"));
echo date_create("last day of -1 month")->format('Y-m-d');
// All the following return 2012-04-01
echo date('Y-m-d', strtotime("first day of -1 month"));
echo date('Y-m-d', strtotime("first day of last month"));
echo date_create("first day of -1 month")->format('Y-m-d');
回答by Gumbo
Try this instead of strtotime:
试试这个而不是strtotime:
mktime(0, (date("n") - 3 + 12) % 12, 1)
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.
这个想法是取当前月份数 ( date("n")),从中减去您想要的月份数(此处-3),将其加 12,然后取模 12。
回答by Gordon
This is known and expected behavior. The reason for this is that a monthis a relative date with no fixed length. From http://www.gnu.org/software/tar/manual/html_node/Relative-items-in-date-strings.html#SEC120
这是已知的和预期的行为。这样做的原因是一个月是一个没有固定长度的相对日期。来自http://www.gnu.org/software/tar/manual/html_node/Relative-items-in-date-strings.html#SEC120
The unit of time displacement may be selected by the string ‘year' or ‘month' for moving by whole years or months. These are fuzzy units, as years and months are not all of equal duration. More precise units are ‘fortnight' which is worth 14 days, ‘week' worth 7 days, ‘day' worth 24 hours, ‘hour' worth 60 minutes, ‘minute' or ‘min' worth 60 seconds, and ‘second' or ‘sec' worth one second. An ‘s' suffix on these units is accepted and ignored.
时间位移的单位可以通过字符串“year”或“month”选择,以整年或整月移动。这些是模糊单位,因为年和月的持续时间并不完全相同。更精确的单位是“两周”,价值 14 天,“周”价值 7 天,“天”价值 24 小时,“小时”价值 60 分钟,“分钟”或“分钟”价值 60 秒,“秒”或'sec' 值一秒钟。接受并忽略这些单位上的“s”后缀。
Consequently (emphasis mine)
因此(强调我的)
The fuzz in units can cause problems with relative items. For example, ‘2003-07-31 -1 month' might evaluate to 2003-07-01, because 2003-06-31 is an invalid date. To determine the previous month more reliably, you can ask for the month before the 15th of the current month. For example:
$ date -R Thu, 31 Jul 2003 13:02:39 -0700 $ date --date='-1 month' +'Last month was %B?' Last month was July? $ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!' Last month was June!
单位中的模糊可能会导致相关项目出现问题。例如,“2003-07-31 -1 月”的计算结果可能为 2003-07-01,因为 2003-06-31 是无效日期。为了更可靠地确定上个月,您可以要求当月 15 号之前的月份。例如:
$ date -R Thu, 31 Jul 2003 13:02:39 -0700 $ date --date='-1 month' +'Last month was %B?' Last month was July? $ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!' Last month was June!
The above could be expressed in PHP as
以上可以用PHP表示为
echo date('M', strtotime(date('Y-m-') . '15 -1 month'));
Also see the commments below http://bugs.php.net/bug.php?id=22486
另请参阅下面的评论http://bugs.php.net/bug.php?id=22486
回答by Fakhruddin Ujjainwala
From PHP 5.3 (Ref: https://bugs.php.net/bug.php?id=44073) and higher you can do:
从 PHP 5.3 (Ref: https://bugs.php.net/bug.php?id=44073) 及更高版本开始,您可以执行以下操作:
"first day of +1 month" or "first day of next month" or even "last day of next month"
" first day of +1 month" 或 " first day of next month" 甚至 " last day of next month"
Example:
例子:
$date = '2015-12-31';
echo date("Y-m-d", strtotime($date ."first day of previous month"));
Will produce: 2015-11-01This does not calculate days on +30 days time basis.
将产生:2015-11-01这不以 +30 天为基础计算天数。
回答by Chris Fazzio
a simple way could be to write it like this
一个简单的方法可能是这样写
echo date("M", strtotime("this year +3 months"));
echo date("M", strtotime("今年+3个月"));
or
或者
echo date("M", strtotime("this month -3 months"));
echo date("M", strtotime("本月-3个月"));
depends what your using it for..
取决于你用它做什么..

