是否有机会在 PHP 中将数据库 utc 时区时间戳记录转换为本地时区?
//example of code
$utc_date = $row['utc_date'];
最佳答案
// common values
$timezone = "Australia/Melbourne";
$tz = new DateTimeZone($timezone);
$dt = new DateTime();
$dt->setTimezone($tz);
// this can run inside a loop or by itself
$utc_date = $row['utc_date'];
$dt->setTimestamp(strtotime($utc_date));
$datetime = $dt->format("Y-m-d H:i:s");
// use the formatted date as required
echo $datetime;
如果您愿意,您可以在循环内创建一个新的 DateTime 对象,或者在循环外创建它并重新使用它。
如果数据库中的日期/时间不是 2019-07-18 10:00:00+00:00
格式,您可能需要添加 +00 :00
到检索日期。
关于php - 如何将存储的数据库 utc 时间戳数据转换为 PHP 中的本地时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57086964/