我正在将时区更改为
Asia/Singapore
处的 config/app.php
,但是当我尝试执行 date("Y-m-d H:i:s");
时,结果仍然是 UTC。
Laravel 中是否有任何内容会覆盖我在
config/app.php
设置的时区?
就这样做:
'timezone' => 'Asia/Singapore'
在
config/app.php
文件中并运行这 3 个命令:
php artisan cache:clear
php artisan view:clear
和
php artisan config:cache
希望这对你有帮助!
将此添加到
config/app.php
文件中:
'timezone' => 'Asia/Singapore'
之后,运行以下命令:
php artisan config:cache
至少在 Laravel 5.8 生成的应用程序框架中,也许其他版本也是如此,
config/app.php
中的时区设置是
'timezone' => 'UTC'
,
所以它会忽略 APP_TIMEZONE
文件中的 .env
。
所以举一个例子
APP_TIMEZONE='Europe/Berlin'
中的 .env
并将 config/app.php
中的行修改为:
'timezone' => env('APP_TIMEZONE', 'UTC')
或者,当确定本地、登台和生产都在同一时区运行时 - 仅将
config/app.php
中的行更改为:
'timezone' => 'Europe/Berlin',
即使在完成了已接受答案中建议的所有操作之后,它对我来说也不起作用。因为我直接从
now()
函数输出结果。所以,而不是做
echo Date::now();
我需要
echo Date::now()->toDateTimeString();