PHP strtotime:获取上个月

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14102625/
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 06:37:43  来源:igfitidea点击:

PHP strtotime: Get previous month

phpdatestrtotime

提问by Martin

Possible Duplicate:
How to get previous month and year relative to today, using strtotime and date?

可能的重复:
如何使用 strtotime 和 date 获取相对于今天的上个月和上一年?

Today is December, 31st but strtotime("-1 months")returns December:

今天是 12 月 31 日,但strtotime("-1 months")返回 12 月:

echo date("Y-m", strtotime("-1 months"));

Same for strtotime("last month")

同为 strtotime("last month")

How can I properly return the previous month (November)?

如何正确返回上个月(11 月)?

Test: http://codepad.viper-7.com/XvMaMB

测试:http: //codepad.viper-7.com/XvMaMB

回答by salathe

strtotime("first day of last month")

The first day ofis the important part as detailed on the Relative Formatsmanual page.

first day of相对格式手册页上详述的重要部分。



Example: http://codepad.viper-7.com/dB35q8(with hard-coded today's date)

示例:http: //codepad.viper-7.com/dB35q8(硬编码今天的日期)

回答by Olaf Dietsche

strtotime("-1 months")would be 2012-11-31, but there's no November, 31st. It is one day past 2012-11-30, which gives 2012-12-01. You will see it, when you do

strtotime("-1 months")2012-11-31,但没有 11 月 31 日。一天过去了2012-11-30,这给了2012-12-01。你会看到的,当你这样做的时候

echo date("Y-m-d", strtotime("-1 months"));

gives as output

作为输出给出

2012-12-01

2012-12-01

See codepad

查看键盘