我有一个正在使用的应用
'timezone' =>'UTC'
在数据库设置中。这适用于所有created_at字段,但是updated_at字段仍然位于错误的时区
如何让 MYSQL 将正确的时区应用于 Updated_at 字段?
这些是我的数据库设置
'default' => 'main_db',
'fetch' => PDO::FETCH_ASSOC,
'connections' => [
'main_db' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => env('DB_DATABASE'),
'username' => 'root',
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'timezone' => env('DB_TIMEZONE')
其中 env('DB_TIMEZONE') 为“UTC”
最佳答案
我们转到文件 config/app.php
并查找此条目:
/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/
'timezone' => 'UTC',
如您所见,UTC 是 Laravel 的默认值。因此,您可以轻松地将其更改为,例如,欧洲/伦敦或美国/洛杉矶 - 查看完整列表 PHP Supported Timezones
如果您更改此设置,很遗憾,旧条目不会自动更改,但新条目将使用您指定时区的 created_at、updated_at 和类似字段进行保存。
关于php - Laravel/MYSQLcreated_at和updated_at正在应用不同的时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53844901/