我在env文件中将时区从UTC更改为Europe/Paris。
当我创建一个DateTime时,时机是正确的(来自巴黎)。
$now = new \DateTime();
echo $now->format('Y-m-d H:i:s'); // correct现在,在我的数据库(MySQL)中,我有一个时间戳字段,我得到了时间-1h:
// database value = 2022-01-31 13:35:42
$expired = new \DateTime($this->expired_at);
echo $expired->format('Y-m-d H:i:s'); // show 2022-01-31 12:35:42为什么从数据库获取的数据看起来像在UTC而不是我的新时区?
在我的PHP项目中,我需要:
SET @@global.time_zone = '+01:00';
SELECT @@global.time_zone, now();当巴黎时间是14:00时,我得到的最后一个查询是PHP:
@@global.time_zone => +01:00
now() => 13:00 // instead of 14:00发布于 2022-01-05 12:57:08
MySQL时区与PHP分离。它们是两种不同的应用程序。若要设置MySQL时区,请在PHP代码中运行此MySQL查询:
SET @@global.time_zone = '+01:00';要确保这是真的,请运行以下查询:
SELECT @@global.time_zone, now();基于注释的编辑
如评论中所述,使用了Laravel框架。因此,下面的文章建议:Is there any way I can set timezone in laravel 4 database.php configuration file?
转到Laravel目录中的app->config->app.php。在第43行,您可以>将默认时区更改为任何您需要的内容。
'timezone' => 'Europe/Paris',https://stackoverflow.com/questions/70593119
复制相似问题