Laravel 更改时区未反映正确时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46423806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel changing timezone not reflecting the correct time
提问by PinoyStackOverflower
I am changing the timezone into Asia/Singapore
at config/app.php
, but when I try to do a date("Y-m-d H:i:s");
the result is still in UTC.
我正在将时区更改为Asia/Singapore
at config/app.php
,但是当我尝试执行 a 时date("Y-m-d H:i:s");
,结果仍为 UTC。
Is there anything in Laravel that overrides that timezone that I set at config/app.php
?
Laravel 中是否有任何内容可以覆盖我设置的时区config/app.php
?
回答by Hiren Gohel
Just do this:
只需这样做:
'timezone' => 'Asia/Singapore'
'timezone' => 'Asia/Singapore'
in config/app.php
file and run this 3 command:
在config/app.php
文件中并运行这 3 个命令:
php artisan cache:clear
php artisan cache:clear
php artisan view:clear
and
php artisan view:clear
和
php artisan config:cache
php artisan config:cache
Hope this helps you!!
希望这对你有帮助!!
回答by Arzoo
Add this in config/app.php
file:
在config/app.php
文件中添加:
'timezone' => 'Asia/Singapore'
After, run this command:
之后,运行此命令:
php artisan config:cache
回答by Schlimmer
At least in the generated application skeleton of Laravel 5.8, maybe other versions too, the timezone setting in config/app.php
is
'timezone' => 'UTC'
,
so it will ignore an APP_TIMEZONE
in the .env
file.
至少在Laravel 5.8的生成的应用程序框架,也许其他版本也一样,时区设置的config/app.php
是
'timezone' => 'UTC'
,这样它会忽略一个APP_TIMEZONE
中.env
文件。
So put an e.g. APP_TIMEZONE='Europe/Berlin'
in .env
and modify the line in config/app.php
to this:
因此,把一个如APP_TIMEZONE='Europe/Berlin'
在.env
和修改行config/app.php
这样的:
'timezone' => env('APP_TIMEZONE', 'UTC')
Or, when it's sure local, staging and production all run in the same timezone - change only the line in config/app.php
to:
或者,当确定本地、暂存和生产都在同一时区运行时 - 仅将行更改config/app.php
为:
'timezone' => 'Europe/Berlin',
回答by Dharma Saputra
The date()
function php won't load configuration from Laravel config/app.php
. To change timezone you should change it via php.ini
on date.timezone
parameter. Don't forget to restart your apache after change it.
该date()
功能PHP将不会从Laravel加载配置config/app.php
。要更改时区,您应该通过php.ini
ondate.timezone
参数更改它。更改后不要忘记重新启动apache。
Or if you wan't more flexibility, there are Carbon
package. It has many usefull functions to deal with datetime operation.
或者,如果您不想获得更大的灵活性,也可以使用Carbon
包。它有许多有用的函数来处理日期时间操作。