<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 2045瀏覽 加載中

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

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

    主站蜘蛛池模板: 堆龙德庆县| 乃东县| 莒南县| 静海县| 茶陵县| 上思县| 富裕县| 通城县| 孙吴县| 特克斯县| 宁远县| 玛纳斯县| 钟祥市| 格尔木市| 溧水县| 册亨县| 桓仁| 奇台县| 南部县| 阳城县| 西乡县| 望谟县| 清涧县| 井研县| 航空| 鲁甸县| 无棣县| 宝坻区| 仙居县| 若尔盖县| 镇安县| 武山县| 景泰县| 凤阳县| 依安县| 化州市| 白银市| 揭西县| 方正县| 奉贤区| 西峡县|