可能是重复问题:
如何使用strtotime和date获取相对于今天的上一个月和年份?
今天是12月31日,但strtotime("-1 months")
返回的是12月:
echo date("Y-m", strtotime("-1 months"));
strtotime("last month")
同理
如何正确返回上个月(11月)?
可能是重复问题:
如何使用strtotime和date获取相对于今天的上一个月和年份?
今天是12月31日,但strtotime("-1 months")
返回的是12月:
echo date("Y-m", strtotime("-1 months"));
strtotime("last month")
同理
如何正确返回上个月(11月)?
strtotime("first day of last month")
first day of
是相对时间格式的重要部分,详见相关格式手册页面。
示例:http://codepad.viper-7.com/dB35q8(以硬编码的今天日期为例)
strtotime("-1 months")
会返回 2012-11-31
,但是实际上不存在11月31日这一天。实际上它代表的是比2012-11-30
晚一天,也就是2012-12-01
。你可以验证一下。echo date("Y-m-d", strtotime("-1 months"));
输出结果为:
2012年12月1日
请参见codepad
strtotime
总是返回一个时间戳(或失败时返回false)。因此,它将返回上个月的第一天的时间戳,但由于您以Y-m
格式输出它,对您来说这没问题。 - Travesty3