[英]How to convert ISO 8601 timestamp without timezone to ISO 8601 timestamp with timezone in PHP?
我的時間戳是這樣的:
$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格式獲取所需本地時間日期的正確方法是什么?
使用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');
您可以設置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.