在Laravel中更改时区

Ami*_*ein 4 php laravel

我想将默认时区从UTC更改为Asia / Tehran,可以在其中进行更改吗?我尝试通过在app.php中更改此代码来尝试,但是它没有用。

'timezone' => 'UTC',
Run Code Online (Sandbox Code Playgroud)

'timezone' => 'Asia/Tehran',
Run Code Online (Sandbox Code Playgroud)

Udh*_*iya 11

转到文件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
Run Code Online (Sandbox Code Playgroud)

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

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

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


Sha*_*rak 6

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

将此添加到.env

TIME_ZONE = 'put_your/timezone_here'
Run Code Online (Sandbox Code Playgroud)

并在/config/app.php更改:

'timezone' => 'UTC',
Run Code Online (Sandbox Code Playgroud)

到:

'timezone' => env('TIME_ZONE', 'UTC'),
Run Code Online (Sandbox Code Playgroud)


小智 5

更新app.php后,运行以下命令并检查

php artisan config:cache
php artisan cache:clear
Run Code Online (Sandbox Code Playgroud)

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

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

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