如果你好奇内部数组何时重置(来自 https://onlinephp.io/c/3ee35)
<?php
$date0 = DateTimeImmutable::createFromFormat('!Y-m-d', '2020-31-31');
var_dump($date0->format('c')); // 2022-07-31T00:00:00+00:00
foreach(range(0,2) as $_)
// 内部错误不会重置
var_dump( join(DateTimeImmutable::getLastErrors()['warnings']) ); // 解析的日期无效
$date1 = DateTimeImmutable::createFromFormat('!Y-m-d', '2020-12-31');
var_dump($date1->format('c')); // 2020-12-31T00:00:00+00:00
// 内部错误已重置
var_dump( empty(DateTimeImmutable::getLastErrors()['warnings']) ); // true
$date2 = DateTimeImmutable::createFromFormat('!Y-m-d', '2020-31-31');
var_dump( join(DateTimeImmutable::getLastErrors()['warnings']) ); // 解析的日期无效
$date3 = new DateTimeImmutable('2023-12-31T00:00:00.000000Z');
// 内部错误已重置
var_dump( empty(DateTimeImmutable::getLastErrors()['warnings']) ); // true
?>
输出
string(25) "2022-07-31T00:00:00+00:00"
string(27) "解析的日期无效"
string(27) "解析的日期无效"
string(27) "解析的日期无效"
string(25) "2020-12-31T00:00:00+00:00"
bool(true)
string(27) "解析的日期无效"
bool(true)