[英]How to convert ISO 8601 timestamp without timezone to ISO 8601 timestamp with timezone in PHP?
My timestamp is like this: 我的时间戳是这样的:
$timestamp = "2018-08-28T08:49:44+00:00";
Easy to convert this to a unix timestamp: 轻松将其转换为Unix时间戳:
$unixtime = strtotime($timestamp);
echo $unixtime; // result: 1535446184
My desired result is to get the timestamp with timezone for my current timezone. 我期望的结果是获取当前时区的时区时间戳。 It should be like this:
应该是这样的:
$timestamp_with_timezone = "2018-08-28T10:49:44+02:00";
However if I do: 但是,如果我这样做:
echo date('c',$unixtime);
The result is again: 结果再次是:
2018-08-28T08:49:44+00:00
What is the correct way to get the desired local time date in ISO 8601 format? 以ISO 8601格式获取所需本地时间日期的正确方法是什么?
Using the DateTime
class: 使用
DateTime
类:
// DateTime automatically parses this format, no need for DateTime::createFromFormat()
$date = new DateTime("2018-08-28T08:49:44+00:00");
// Set the timezone (mine here)
$date->setTimezone(new DateTimeZone('Europe/Paris'));
// Output: 2018-08-28T10:49:44+02:00
echo $date->format('c');
You can set the TimeZone of the DateTime object: 您可以设置DateTime对象的TimeZone:
$timestamp = "2018-08-28T08:49:44+00:00";
$date = date_create($timestamp);
$date->setTimezone(new DateTimeZone('Europe/Amsterdam'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.