1 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
這是由于DateTime構(gòu)造函數(shù)處理first day of相對(duì)時(shí)間的方式不一致:
$month_start = new DateTime("first day of this month");
echo $month_start->format('Y-m-d H:i:s') . "\n";
$month_start = new DateTime("first day of March");
echo $month_start->format('Y-m-d H:i:s') . "\n";
截至2020 年3 月 22 日的輸出(演示):
2020-03-01 03:49:52
2020-03-01 00:00:00
請(qǐng)注意,該first day of this month變量具有非零時(shí)間部分。但是,當(dāng)您計(jì)算該$month_end值時(shí),您會(huì)得到一個(gè)零時(shí)間:
$month_end = new DateTime("last day of April");
echo $month_end->format('Y-m-d H:i:s') . "\n";
輸出(演示):
2020-04-30 00:00:00
所以代碼中的循環(huán)失敗了,因?yàn)?month_start到達(dá)2020-04-30非零時(shí)間,where$month_end有一個(gè)零時(shí)間,因此<=比較失敗。
您可以通過向第一個(gè)值添加時(shí)間部分以強(qiáng)制其為 0 來解決此問題:
$month_start = new DateTime("first day of this month 00:00");
然后您的循環(huán)將按預(yù)期工作:Demo on 3v4l.org。
- 1 回答
- 0 關(guān)注
- 110 瀏覽
添加回答
舉報(bào)