我的时间戳是这样的:
$timestamp = "2018-08-28T08:49:44+00:00";
易于将其转换为unix时间戳:
$unixtime = strtotime($timestamp);
echo $unixtime; // result: 1535446184
我想要的结果是获取当前时区的带时区的时间戳。应该是这样的:
$timestamp_with_timezone = "2018-08-28T10:49:44+02:00";
但是,如果我这样做:
echo date('c',$unixtime);
结果再次是:
2018-08-28T08:49:44+00:00
以ISO 8601格式获取所需本地时间日期的正确方法是什么?
答案 0 :(得分:3)
使用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');
答案 1 :(得分:1)
您可以设置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";