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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:43:35  来源:igfitidea点击:

Laravel changing timezone not reflecting the correct time

phplaravellaravel-5

提问by PinoyStackOverflower

I am changing the timezone into Asia/Singaporeat config/app.php, but when I try to do a date("Y-m-d H:i:s");the result is still in UTC.

我正在将时区更改为Asia/Singaporeat 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.phpfile and run this 3 command:

config/app.php文件中并运行这 3 个命令:

php artisan cache:clear

php artisan cache:clear

php artisan view:clearand

php artisan view:clear

php artisan config:cache

php artisan config:cache

Hope this helps you!!

希望这对你有帮助!!

回答by Arzoo

Add this in config/app.phpfile:

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.phpis 'timezone' => 'UTC', so it will ignore an APP_TIMEZONEin the .envfile.

至少在Laravel 5.8的生成的应用程序框架,也许其他版本也一样,时区设置的config/app.php'timezone' => 'UTC',这样它会忽略一个APP_TIMEZONE.env文件。

So put an e.g. APP_TIMEZONE='Europe/Berlin'in .envand modify the line in config/app.phpto 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.phpto:

或者,当确定本地、暂存和生产都在同一时区运行时 - 仅将行更改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.inion date.timezoneparameter. Don't forget to restart your apache after change it.

date()功能PHP将不会从Laravel加载配置config/app.php。要更改时区,您应该通过php.iniondate.timezone参数更改它。更改后不要忘记重新启动apache。

Or if you wan't more flexibility, there are Carbonpackage. It has many usefull functions to deal with datetime operation.

或者,如果您不想获得更大的灵活性,也可以使用Carbon包。它有许多有用的函数来处理日期时间操作。