简体   繁体   English

将 influxdb 时间格式转换为 ISO8601 格式

[英]Convert influxdb time format to ISO8601 format

When using the influxdb PHP client I see this time format with nanosecond precision:使用 influxdb PHP 客户端时,我看到这种时间格式具有纳秒精度:

2020-02-06T17:26:38.277740846Z

PHP DateTime does not seem to understand the format: PHP DateTime 似乎不理解格式:

$date = DateTime::createFromFormat("Y-m-d\TH:i:s.u?",$time);

I get false as the return value.我得到false作为返回值。

How can I convert this to ISO8601 or a custom format?如何将其转换为 ISO8601 或自定义格式?

DateTime only accepts a maximum of 6 digits for the microseconds. DateTime 仅接受最多 6 位的微秒数。 The letter Z represents a time zone.字母 Z 代表一个时区。 If the surplus digits are removed, the time zone must be set to Z with the 3rd parameter.如果去除多余的数字,则必须使用第三个参数将时区设置为 Z。

$time = '2020-02-06T17:26:38.277740846Z';

$dateTime = DateTime::createFromFormat('Y-m-d\TH:i:s.u???\Z',$time,new DateTimeZone('Z'));

Converting the DateTime object to a custom format is then very easy.将 DateTime 对象转换为自定义格式非常容易。

echo $dateTime->format("Y-m-d H:i:s.v");
//2020-02-06 17:26:38.277

As PHP dates don't handle more than the microseconds, you can ignore the rest of your string after the 6 digits of the microseconds with the + specifier由于 PHP 日期处理的时间不超过微秒,因此您可以使用+说明符忽略微秒的 6 位数字之后的其余字符串

From the documentation :文档

+ +

If this format specifier is present, trailing data in the string will not cause an error, but a warning instead如果存在此格式说明符,则字符串中的尾随数据不会导致错误,而是会导致警告

Use DateTime::getLastErrors() to find out whether trailing data was present.使用 DateTime::getLastErrors() 找出是否存在尾随数据。

If your dates always end with 'Z', you can force the UTC timezone :如果您的日期总是以“Z”结尾,您可以强制使用 UTC 时区:

$time = '2020-02-06T17:26:38.277740846Z' ;
$date = DateTime::createFromFormat("Y-m-d\TH:i:s.u+", $time, new DateTimeZone('UTC'));
echo $date->format('c'); // 2020-02-06T17:26:38+00:00

Well, most of the time, people don't need so much precision on nanoseconds.嗯,大多数时候,人们不需要那么高的纳秒精度。 In my case, just seconds.就我而言,只需几秒钟。 In my experience, ignoring precision setup on InfluxDb measurements(most users will do that...), results in inconsistent number of nanoseconds digits, leading to failure in conversion.根据我的经验,忽略 InfluxDb 测量的精度设置(大多数用户会这样做...),会导致纳秒数不一致,从而导致转换失败。 So, because PHP DateTime fails on inconsistent formats, why not restrict nanoseconds to 6 digits by default?那么,因为 PHP DateTime 在不一致的格式上失败,为什么不默认将纳秒限制为 6 位呢?

I did this:我这样做了:

$influxDbDate ="2020-02-06T17:26:38.277740846Z"
$outformat = '%F %T'; //equivalent of 'Y-m-d H:i:s' or you could get just date with 'Y-m-d' and so on...
$datePortions = explode('.', $influxDbDate);
$remadeTime = $datePortions[0] . '.' . substr(explode('Z', $datePortions[1])[0], 0, 6) . 'Z';
$dateTime = DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $remadeTime, new DateTimeZone('Z'));
if ($dateTime)
  $result = strftime($outformat, $dateTime->getTimestamp());
else
$result = $remadeTime . ' is not InfluxDb timeformat';

From PHP8, the DateTime constructor will no longer choke on this input format.从 PHP8 开始,DateTime 构造函数将不再阻塞这种输入格式。 It will truncate the nanosecond precision to microseconds (6 places) though.它会将纳秒精度截断为微秒(6 位)

Code: ( Demo )代码:(演示

$dt = '2020-02-06T17:26:38.277740846Z';
echo (new DateTime($dt))->format('Y-m-d H:i:s');
// 2020-02-06 17:26:38

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM