PHP Carbon 从字符串格式确定日期类型 [英] PHP Carbon determine date type from string format
问题描述
我有一个包含设备数据的数据库,每种设备类型的消息时间戳可能不同.
I have a database that contains device data and each device type may timestamp its messages differently.
具体来说,我有一个包含此时间戳的字符串字段,采用以下两种格式:
Specifically, I have a string field containing this timestamp, in the following two formats:
2020-01-29T01:30:00.000+11:00
和
2020-01-29T00:30:01.000Z
我正在尝试将这些字符串转换为 UTC 日期,但我正在努力识别 Carbon 的格式.例如,这会给我数据丢失"错误.
I am trying to convert these strings to UTC dates, but I am struggling with identifying the formats for Carbon. For example, this gives me "data missing" error.
$d = Carbon\Carbon::createFromFormat('Y-m-d\TH:i:s.uP T', '2020-01-29T01:30:00.000+11:00', 'UTC');
我感谢任何建议.谢谢
推荐答案
这是一个简单的错误:您有两个时区标识符,P 与格林威治时间 (GMT) 的差异,小时和分钟之间带有冒号(在 PHP 5.1 中添加).3)
和 T 时区缩写
.选择一个,两个都可以:
It's a simple typo: You have two timezone identifiers, P Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3)
and T Timezone abbreviation
. Pick one, both will work:
$d = Carbon\Carbon::createFromFormat('Y-m-d\TH:i:s.uP', '2020-01-29T01:30:00.000Z', 'UTC');
// object(Carbon\Carbon)(
// 'date' => '2020-01-29 01:30:00.000000',
// 'timezone_type' => 2,
// 'timezone' => 'Z'
// )
$d = Carbon\Carbon::createFromFormat('Y-m-d\TH:i:s.uT', '2020-01-29T01:30:00.000Z', 'UTC');
// object(Carbon\Carbon)(
// 'date' => '2020-01-29 01:30:00.000000',
// 'timezone_type' => 2,
// 'timezone' => 'Z'
// )
$d = Carbon\Carbon::createFromFormat('Y-m-d\TH:i:s.uT', '2020-01-29T01:30:00.000+11:00', 'UTC');
// object(Carbon\Carbon)(
// 'date' => '2020-01-29 01:30:00.000000',
// 'timezone_type' => 1,
// 'timezone' => '+11:00'
// )
$d = Carbon\Carbon::createFromFormat('Y-m-d\TH:i:s.uP', '2020-01-29T01:30:00.000+11:00', 'UTC');
// object(Carbon\Carbon)(
// 'date' => '2020-01-29 01:30:00.000000',
// 'timezone_type' => 1,
// 'timezone' => '+11:00'
// )
这篇关于PHP Carbon 从字符串格式确定日期类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!