首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP strtotime:获取上个月

PHP strtotime:获取上个月
EN

Stack Overflow用户
提问于 2012-12-31 22:14:55
回答 2查看 45.9K关注 0票数 20

可能重复:

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

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

代码语言:javascript
复制
echo date("Y-m", strtotime("-1 months"));

strtotime("last month")也是如此

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-31 22:17:54

代码语言:javascript
复制
strtotime("first day of last month")

Relative Formats手册页所述,first day of是重要的部分。

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

票数 35
EN

Stack Overflow用户

发布于 2012-12-31 22:26:23

strtotime("-1 months")应该是2012-11-31,但没有11月31号。这比提供2012-12-012012-11-30晚了一天。当你这样做的时候,你会看到它。

代码语言:javascript
复制
echo date("Y-m-d", strtotime("-1 months"));

以输出形式给出

2012-12-01

请参阅codepad

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14102625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档