<button id="g80sy"></button>
  • <abbr id="g80sy"></abbr>
  • <button id="g80sy"></button>
    <rt id="g80sy"><delect id="g80sy"></delect></rt>
  • PHP strtotime小BUG

    程序猿 2021-07-31 16:37:11 1786瀏覽 加載中

    今天看網站統計發現上月和這月的數據一樣。用的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"

    這樣就能返回到上一個月了。

    標簽:
    最后修改:2025-03-23 19:20:46

    非特殊說明,本博所有文章均為博主原創。

    主站蜘蛛池模板: 五莲县| 铜鼓县| 嘉兴市| 定兴县| 巴中市| 瓮安县| 永仁县| 凤凰县| 遂溪县| 无棣县| 东丽区| 绥江县| 阿巴嘎旗| 边坝县| 鄂伦春自治旗| 锦屏县| 巍山| 奉节县| 肥西县| 宝应县| 建平县| 甘南县| 昌平区| 乐至县| 枣阳市| 岢岚县| 师宗县| 敦化市| 富阳市| 广汉市| 碌曲县| 太原市| 九台市| 公主岭市| 夏津县| 常德市| 田林县| 沅陵县| 夏邑县| 乳源| 长葛市|