[英]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.