[英]Convert PHP Mongo UTC Date From Milliseconds To Date for TimeZone
I have a date in UTC / Epoch format from a mongo database (1659052800000) and I'm trying to convert it to a friendly format using PHP for the PST timezone.我有一个来自 mongo 数据库(1659052800000)的 UTC / Epoch 格式的日期,我正在尝试使用 PHP 将其转换为 PST 时区的友好格式。
Here's what I've tried.这是我尝试过的。
date_default_timezone_set('PST');
$this->vars[dateOfEvent] = date('Y-m-d', 1659052800000);
and和
date_default_timezone_set('PST');
$dt = new DateTime(1659052800000);
$this->vars[dateOfEvent] = $dt->format('Y-m-d');
but neither give me the correct date.但都没有给我正确的日期。 I'm expecting it to out "2022-07-28"
我期待它出“2022-07-28”
You can set the timezone temporarily for the DateTime object.您可以为 DateTime object 临时设置时区。 Also you need to set timestamp divided by 1000 or remove the last three zeroes .
您还需要设置时间戳除以 1000或删除最后三个零。
$dt = new DateTime();
$dt->setTimestamp(1659052800000 / 1000);
$dt->setTimezone(new DateTimeZone('PST'));
echo $dt->format('Y-m-d');
prints印刷
2022-07-28
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.