PHP Strtotime -1个月 -2个月

发布于 2024-07-29 16:39:33 字数 310 浏览 7 评论 0原文

昨天工作正常,没有更改代码。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

北笙凉宸 2024-08-05 16:39:33

它可能与错误 #44073 有关,

您可以尝试使用以下方法:

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";

(在 strtotime 的评论部分找到解决方案;直接链接)

输出:

Apr
May
Jun
Jul

有点“作弊”日期格式和月份名称等等......

It might be related to bug #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...

忘你却要生生世世 2024-08-05 16:39:33

戈登正确地识别了这个问题,但我想提供另一个有用但不那么技术性的解决方案。 只需在时间中使用“第一天”或“最后一天”即可。 例如,以下示例在每月 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');

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:

// 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');
对岸观火 2024-08-05 16:39:33

试试这个而不是 strtotime

mktime(0, (date("n") - 3 + 12) % 12, 1)

这个想法是获取当前月份数字 (date("n")),从中减去您想要的月份数(这里< code>-3),加上 12,然后对 12 求模。

Try this instead of 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.

伤感在游骋 2024-08-05 16:39:33

这是已知且预期的行为。 原因是是一个没有固定长度的相对日期。 来自 http://www. gnu.org/software/tar/manual/html_node/Relative-items-in-date-strings.html#SEC120

时间位移的单位可以通过字符串“年”或“月”来选择,以整年或整月移动。 这些是模糊单位,因为年和月的持续时间并不相同。 更精确的单位是“两周”,相当于 14 天,“周”相当于 7 天,“天”相当于 24 小时,“小时”相当于 60 分钟,“分钟”或“分钟”相当于 60 秒,“秒”或“秒”值一秒。 这些单位上的 's' 后缀会被接受并被忽略。

因此(强调我的)

单位中的模糊可能会导致相关项目出现问题。 例如,“2003-07-31 -1 月”的计算结果可能为 2003-07-01,因为 2003-06-31 是无效日期。 为了更可靠地确定上个月,您可以询问当月 15 日之前的月份。 例如:

<前><代码>$日期-R
2003 年 7 月 31 日星期四 13:02:39 -0700
$ date --date='-1 月' +'上个月是 %B?'
上个月是七月?
$ date --date="$(date +%Y-%m-15) -1 个月" +'上个月是 %B!'
上个月是六月!

上述内容可以用 PHP 表示为

echo date('M', strtotime(date('Y-m-') . '15 -1 month'));

另请参阅下面的注释 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

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.

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!

The above could be expressed in PHP as

echo date('M', strtotime(date('Y-m-') . '15 -1 month'));

Also see the commments below http://bugs.php.net/bug.php?id=22486

笔芯 2024-08-05 16:39:33

从 PHP 5.3 开始(参考:https://bugs.php.net/bug.php?id =44073)及更高版本,您可以执行以下操作:

+1 个月的第一天”或“下个月的第一天”甚至“最后一天下个月的某一天

示例:

$date = '2015-12-31';

echo date("Y-m-d", strtotime($date ."first day of previous month"));

将生成: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:

$date = '2015-12-31';

echo date("Y-m-d", strtotime($date ."first day of previous month"));

Will produce: 2015-11-01 This does not calculate days on +30 days time basis.

GRAY°灰色天空 2024-08-05 16:39:33

一种简单的方法可能是像这样写

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文