无法使用PHP格式化UTC日期

时间:2019-07-24 02:32:13

标签: php

使用php应用程序,我有一个像这样的日期'2019-07-22T04:44:43.843710438Z'并且我不知道如何将其转换为这样的日期7/22/2019 3:11下午。 strtotime只是返回false,我尝试了几个时区,但没有任何效果。这是我一直在尝试的东西的便笺簿

 $timestamp = '2019-07-22T04:44:43.843710438Z';
//$reset = date_default_timezone_get();
//date_default_timezone_set('America/New_York');
//$stamp = strtotime($timestamp);
//date_default_timezone_set($reset);

$result = date_parse_from_format("j F Y G:i", $timestamp);

2 个答案:

答案 0 :(得分:3)

使用DateTime类(https://www.php.net/manual/en/class.datetime.php)可以使您的工作更加轻松(无法抗拒)。

您遇到了问题,因为您拥有的值不是时间戳记,因此您无法像使用它那样使用它。

原始时间值使用纳秒级精度(9dp),PHP不支持。为了在DateTime中使用该时间字符串,您首先必须将其截断为微秒(6dp)。然后,它具有一种将以所需格式(https://www.php.net/manual/en/function.date.php)返回值的方法。

bundler

答案 1 :(得分:0)

如果将时间戳记限制为2个小数位,则

使用 strtotime 有效。请尝试以下操作:

$time      = "2019-07-22T04:44:43.843710438Z";
$temp_str = rtrim($time, "Z");
$temp_arr = explode(":", $temp_str);
$temp_str = number_format($temp_arr[count($temp_arr)-1], 8);
$time     = $temp_arr[0] . ":" . $temp_arr[1] . ":" . $temp_str . "Z";

$timestamp = strtotime($time);
echo date("m/d/Y H:i a", $timestamp);