PHP strtotime小BUG

程序猿 2021-07-31 16:37:11 1154浏览 加载中

今天看网站统计发现上月和这月的数据一样。用的whereMonth 时间查询

->whereMonth('create_time')  // 本月
->whereMonth('create_time','last month')  // 上月

看着代码没啥问题,往上翻找到了

        if (in_array($month, ['this month', 'last month'])) {
            $month = date('Y-m', strtotime($month));
        }

于是打印了

dump(date("Y-m-d",strtotime("this month")));
dump(date('Y-m-d', strtotime("last month")));

结果

^ "2021-07-31"
^ "2021-07-01"

这不是同一月么。这是strtotime函数的BUG吧。在31号的时候返回不到上一月。

在网上巴拉巴拉,发现可以这样写

dump(date("Y-m-d",strtotime("first day of this month")));
dump(date('Y-m-d', strtotime("first day of last month")));

结果

^ "2021-07-01"
^ "2021-06-01"

这样就能返回到上一个月了。

标签:
最后修改:2024-04-20 22:28:03

非特殊说明,本博所有文章均为博主原创。