第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

PHP 日期未正確迭代

PHP 日期未正確迭代

PHP
炎炎設(shè)計(jì) 2022-11-12 10:00:58
我試圖迭代從first day of this month到的日期last day of April:$month_start = new DateTime("first day of this month");$month_end = new DateTime("last day of April");while ($month_start <= $month_end) {    echo $month_start->format("Y-m-d\n");    $month_start->add(new DateInterval('P1D'));}截至2020 年3 月 22 日的輸出(演示):2020-03-012020-03-022020-03-03...2020-04-272020-04-282020-04-29如您所見,盡管<=在比較中使用了 ,但輸出中缺少 4 月 30 日。這是為什么?
查看完整描述

1 回答

?
慕的地8271018

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。



查看完整回答
反對(duì) 回復(fù) 2022-11-12
  • 1 回答
  • 0 關(guān)注
  • 110 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)