【问题标题】:PHP strtotime: Get previous month [duplicate]PHP strtotime:获取上个月[重复]
【发布时间】:2012-12-15 16:09:55
【问题描述】:

可能重复:
How to get previous month and year relative to today, using strtotime and date?

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

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

strtotime("last month") 也一样

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

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

【问题讨论】:

    标签: php date strtotime


    【解决方案1】:
    strtotime("first day of last month")
    

    first day ofRelative Formats 手册页中详述的重要部分。


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

    【讨论】:

    • 谢谢。非常误导!听起来它会返回“1”或工作日。
    • @Martin 试试看
    • @Martin: strtotime 总是返回一个时间戳(失败时返回 false)。所以它返回上个月第一天的时间戳,但是由于您以Y-m 格式输出它,这对您来说很好。
    • “下个月的第一天”在 PHP 5.2.4 中不起作用
    • 很多东西在古语言版本中是行不通的。
    【解决方案2】:

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

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

    作为输出给出

    2012-12-01

    codepad

    【讨论】:

    • 谢谢,这就解释了。
    猜你喜欢
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多