亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

DateTime::setTime() 期望參數 1 為 int,給定字符串,但在嘗試觀察時消失

DateTime::setTime() 期望參數 1 為 int,給定字符串,但在嘗試觀察時消失

PHP
慕妹3242003 2023-06-24 15:11:40
我在 php7.4 中有以下代碼,用于從另一個創建日期    $date = clone $regularCourse->getNextCronExecutionDate();    $date->modify('+ 3 days');    $date->setTime($date->format('H'), $date->format('i'), 0, 0);我已經在本地和生產中對其進行了測試,并且它曾經工作得很好。突然間它開始失敗。與錯誤DateTime::setTime() expects parameter 1 to be int, string given它會定期且可預測地失敗,因為我的哨兵給了我 4000 次該事件的發生次數(這是一個每分鐘運行一次的 cron 任務,哨兵向我顯示過去幾天該錯誤每小時發生 60 次)但 !現在我已經添加了一些調試來顯示該值,它不再失敗我使用的代碼     // Added to debug some courses failing     ob_start();     var_dump($date);     $dumped_message= ob_get_clean();     \Sentry\addBreadcrumb(         new \Sentry\Breadcrumb(             \Sentry\Breadcrumb::LEVEL_INFO,             \Sentry\Breadcrumb::TYPE_DEFAULT,             'error_reporting',             "course Id " . $regularCourse->getId()         )     );     \Sentry\addBreadcrumb(         new \Sentry\Breadcrumb(             \Sentry\Breadcrumb::LEVEL_INFO,             \Sentry\Breadcrumb::TYPE_DEFAULT,             'error_reporting',             $dumped_message         )     );我不知道 var_dump-ing 變量是否會產生一些副作用?所以我的問題這個錯誤什么時候發生?為什么我的調試代碼使問題消失?
查看完整描述

1 回答

?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

如果您使用,declare(strict_types=1)則需要注意類型:

  • DateTime::setTime()需要整數:

    public?DateTime::setTime?(?int?$hour?,?int?$minute?[,?int?$second?=?0?[,?int?$microseconds?=?0?]]?)?:?DateTime
  • DateTime::format()返回字符串:

    public?DateTime::format?(?string?$format?)?:?string

在這種情況下,您可以直接轉換為 int,盡管這可以輕松掩蓋其他格式錯誤:

declare(strict_types=1);

$date = new \DateTime();

// Correct

$date->setTime((int)$date->format('H'), (int)$date->format('i'), 0, 0);

// Typo, no error thrown becuase `Sunday` casts to 0

$date->setTime((int)$date->format('l'), (int)$date->format('i'), 0, 0);

... 盡管


$date = new \DateTime();

$date->setTime($date->format('l'), $date->format('i'), 0, 0);

// DateTime::setTime() expects parameter 1 to be int, string given


查看完整回答
反對 回復 2023-06-24
  • 1 回答
  • 0 關注
  • 208 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號