【问题标题】:strtotime 'next month' not acting as expected (today that is)strtotime 'next month' 未按预期运行(即今天)
【发布时间】:2011-05-31 04:10:39
【问题描述】:

到目前为止,这只发生在 31 日

echo date('F',strtotime('this month')); //May
echo date('F',strtotime('next month'));//July
echo date('F',strtotime('+1 month'));//July

据我所知,六月是在五月之后。但我猜 php 只是从现在开始添加 31 天,跳过整整一个月。

无论多长时间,我怎样才能安全地度过下个月?理想情况下使用 strttotime

edit忘了说,为什么我希望使用 strtotime 是因为我正在使用搜索框来查找解释用户输入 strtotime 的事件

【问题讨论】:

标签: php date


【解决方案1】:

最健壮的方法可能是:

date('F', mktime(0, 0, 0, date('n') + 1, 1))

【讨论】:

    【解决方案2】:

    你也可以使用:

    echo date('Y-m-d', strtotime("first day of -1 month")); 
    

    【讨论】:

      【解决方案3】:

      这是因为 PHP 处理日期的方式中的日期溢出。它只是将月份增加 1。没有 6 月 31 日,因此它通过添加一天然后将其放入 7 月来解决它。尝试将其转换为月份的第一天,然后增加月份。

      这是一个错误,但由于它的方式和存在多长时间,它被标记为 wontfix。 https://bugs.php.net/bug.php?id=66988

      【讨论】:

        猜你喜欢
        • 2011-10-31
        • 1970-01-01
        • 2011-05-08
        • 2014-11-12
        • 1970-01-01
        • 2020-06-28
        • 2012-02-18
        • 2018-01-18
        • 2012-06-14
        相关资源
        最近更新 更多