简体   繁体   English

如何将纪元时间戳转换为 UTC 格式,如 PHP 中的 2014-06-25T14:38:52.359Z

[英]How can I convert a epoch time stamp to a UTC format like 2014-06-25T14:38:52.359Z in PHP

I am looking to convert an EPOCH timestamp (like 1372190184) to a format 2014-06-25T14:38:52.359Z.我希望将 EPOCH 时间戳(如 1372190184)转换为 2014-06-25T14:38:52.359Z 格式。

I have tried the following code, but the format I get is different from what I need.我试过下面的代码,但是我得到的格式和我需要的不一样。

$start = new DateTime(date('r', '1372190184'));
$startDateText = $start->format('Y-m-dTH:i:sZ');
var_dump($startDateText);
exit();

But I get the output as string(30) "2013-06-25GMT+020021:56:247200" which is different from what I expect.但我得到的 output 为 string(30) "2013-06-25GMT+020021:56:247200" 这与我的预期不同。

You forgot the backslashes in your format, and the dollar sign before startDateText in the dump:您忘记了格式中的反斜杠,以及转储中 startDateText 之前的美元符号:

$start = new DateTime(date('r', '1372190184'));
$startDateText = $start->format('Y-m-d\TH:i:s\Z');
var_dump($startDateText);

Also, if you're looking for microseconds, add the u format character.此外,如果您正在寻找微秒,请添加u格式字符。

You should be setting the date_default_timezone_set to UTC for your desired output. Format as you wish.您应该为所需的 output 将date_default_timezone_set设置为 UTC。根据需要设置格式。 And make sure to escape special characters in the format.并确保转义格式中的特殊字符。

date_default_timezone_set('UTC');
$epoch = 1340000000;
echo gmdate('r', $epoch);

You can convert to UTC format date from a date string, for example:您可以将日期字符串转换为 UTC 格式日期,例如:

$date = '2022-05-02 11:50:00';
$date = date('Y-m-d\TH:i:s\Z', strtotime($date));

echo $date;

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

相关问题 如何在PHP中将2014-06-24T22:33:49.180Z格式转换为Ymd H:i:s - How to convert 2014-06-24T22:33:49.180Z format into Y-m-d H:i:s in PHP 我如何使用 php/laravel 这样的格式 2017-10-14T22:11:20+0000 转换 UTC 中的日期时间 - How do i convert datetime which is in UTC using php/laravel like this format 2017-10-14T22:11:20+0000 PHP:如何以下列格式显示日期时间(2015-01-05T06:27:50.000Z)? - PHP: How to display date time in following format (2015-01-05T06:27:50.000Z )? 使用PHP将WMI UTC时间转换为Unix时间戳 - Convert WMI UTC time to unix time stamp using PHP strtotime不会转换此2014-06-03T07:00:00.000Z - strtotime wont convert this 2014-06-03T07:00:00.000Z 如何在PHP中获取输出日期格式为2016-05-03T01:38:54.003Z? - How to get the output date format as 2016-05-03T01:38:54.003Z in PHP? PhP代码将时间戳转换为JSON格式 - PhP code to convert time stamp to JSON format PHP:无法将MySQL DateTime转换为Epoch(具有正确的一天中的时间) - PHP: Can't Convert MySQL DateTime to Epoch (with Correct Time of Day) PHP:如何找到值“2022-06-24T15:04:37.000000Z”和“2022-06-24 15:05:15”之间的时间差,例如使用 strtotime()? - PHP: How can one find the time difference in seconds between values "2022-06-24T15:04:37.000000Z" and "2022-06-24 15:05:15", e.g. using strtotime()? 如何将当地时间转换成UTC格式? - How to convert local time to UTC format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM