无法解析时间字符串祖鲁时间
Failed to parse time string zulu time
尝试使用 Carbon 解析日期时间 (Zulu) 时,出现以下错误:
$t = '2021-06-01T21:29:55.155257426Z';
$r = Carbon::parse($t)->setTimezone('UTC');
dd($r);
Could not parse '2021-06-01T21:29:55.155257426Z': DateTime::__construct(): Failed to parse time string (2021-06-01T21:29:55.155257426Z) at position 0 (2): The timezone could not be found in the database
但是,如果我删除最后 3 位数字,一切正常,例如:
$t = '2021-06-01T21:29:55.155257Z';
$r = Carbon::parse($t)->toDateTimeString('microsecond');
dd($r);
"2021-06-01 21:29:55.155257"
当我有 9 位数字 (.155257426Z) 时,我不确定如何解析微秒。任何帮助表示赞赏。谢谢。
PHP 支持 Carbon 的 DateTime 只能处理最多六位数的小数秒。
以下将 trim 最多减少到六个:
function trim_nanoseconds($t) {
return preg_replace_callback(
'/\.(\d+)(.*)$/',
function($a){
return sprintf('.%s%s', substr($a[1], 0, 6), $a[2]);
},
$t
);
}
$t = '2021-06-01T21:29:55.123456789Z';
var_dump(fix_nanoseconds($t));
输出:
string(27) "2021-06-01T21:29:55.123456Z"
$t = '2021-06-01T21:29:55.155257426Z';
$date = Carbon::parse(preg_replace('/(\d\.\d{6})\d+/', '', $t));
尝试使用 Carbon 解析日期时间 (Zulu) 时,出现以下错误:
$t = '2021-06-01T21:29:55.155257426Z';
$r = Carbon::parse($t)->setTimezone('UTC');
dd($r);
Could not parse '2021-06-01T21:29:55.155257426Z': DateTime::__construct(): Failed to parse time string (2021-06-01T21:29:55.155257426Z) at position 0 (2): The timezone could not be found in the database
但是,如果我删除最后 3 位数字,一切正常,例如:
$t = '2021-06-01T21:29:55.155257Z';
$r = Carbon::parse($t)->toDateTimeString('microsecond');
dd($r);
"2021-06-01 21:29:55.155257"
当我有 9 位数字 (.155257426Z) 时,我不确定如何解析微秒。任何帮助表示赞赏。谢谢。
PHP 支持 Carbon 的 DateTime 只能处理最多六位数的小数秒。
以下将 trim 最多减少到六个:
function trim_nanoseconds($t) {
return preg_replace_callback(
'/\.(\d+)(.*)$/',
function($a){
return sprintf('.%s%s', substr($a[1], 0, 6), $a[2]);
},
$t
);
}
$t = '2021-06-01T21:29:55.123456789Z';
var_dump(fix_nanoseconds($t));
输出:
string(27) "2021-06-01T21:29:55.123456Z"
$t = '2021-06-01T21:29:55.155257426Z';
$date = Carbon::parse(preg_replace('/(\d\.\d{6})\d+/', '', $t));