在 Laravel 中更改时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54087271/
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 18:14:36  来源:igfitidea点击:

Change timezone in laravel

phplaravel

提问by Amirhossein

I want change the default timezone from UTC to Asia/Tehran where I can change it? I tried by changing this code in app.php but it did not work.

我想将默认时区从 UTC 更改为亚洲/德黑兰,我可以在哪里更改?我尝试在 app.php 中更改此代码,但没有奏效。

'timezone' => 'UTC',

to

'timezone' => 'Asia/Tehran',

采纳答案by Amirhossein

After update app.php run below command and check

更新 app.php 后运行下面的命令并检查

php artisan config:cache
php artisan cache:clear

You can create below type of route for clear cache in laravel

您可以在laravel中创建以下类型的路线以清除缓存

Route::get('/clear-cache', function() {

    $configCache = Artisan::call('config:cache');
    $clearCache = Artisan::call('cache:clear');
    // return what you want
});

回答by Udhav Sarvaiya

go to the file config/app.phpand look for this entry:

转到文件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' => 'Asia/Tehran', //There will be default 'UTC' here

As you can see, UTC is a default value for Laravel. So you can easily change it here to, like:

如您所见,UTC 是 Laravel 的默认值。因此,您可以在此处轻松将其更改为:

'timezone' => 'Asia/Tehran',- See full list PHP Supported Timezones

'timezone' => 'Asia/Tehran',- 查看完整列表PHP 支持的时区

After changes app.phpyou should run this command php artisan config:cache

更改后,app.php您应该运行此命令php artisan config:cache

回答by Sharak

I wonder why Laravel team didn't put this inside .env. It seems like best place for parameter like that.

我想知道为什么 Laravel 团队没有把它放在.env 中。看起来像这样的参数的最佳位置。

Add this to .env:

将此添加到.env

TIME_ZONE = 'put_your/timezone_here'

and in /config/app.phpchange:

并在/config/app.php更改:

'timezone' => 'UTC',

to:

到:

'timezone' => env('TIME_ZONE', 'UTC'),