Lumen timezone time zone setting method of is 8 hours slow

  • 2021-09-11 19:41:21
  • OfStack

According to the experience of Laravel 4.x and 5.0, it is only necessary to set the parameter 'timezone' to 'PRC' in config/app. php, and find the config directory of Lumen under the path /vendor/laravel/lumen-framework/config. However, there is no timezone parameter option in config/app. php parameter option, which is also invalid after manual addition.

Then I think of the. env file of Laravel 5, and I find that there is no option for timezone setting in the. env file of Lumen.

Going back to the config directory and looking at the settings in config/database. php, the default configuration for mysql is as follows:


'mysql' => [
 'driver'  => 'mysql',
 'host'   => env('DB_HOST', 'localhost'),
 'port'   => env('DB_PORT', 3306),
 'database' => env('DB_DATABASE', 'forge'),
 'username' => env('DB_USERNAME', 'forge'),
 'password' => env('DB_PASSWORD', ''),
 'charset'  => 'utf8',
 'collation' => 'utf8_unicode_ci',
 'prefix'  => env('DB_PREFIX', ''),
 'timezone' => env('DB_TIMEZONE','+00:00'),
 'strict'  => false,
],

Here is a database timezone settings, the default +00:00, that is, UTC time, changed to +08:00 problem solving. Because the project has the. env configuration file enabled, it ends up adding 1 line to the. env file

DB_TIMEZONE=+08:00

Database timezone problem solving.

Although the timezone problem of the database has been solved, the timezone problem of app has not been solved. Search the lumen project globally and find the place where timezone is used. /vendor/laravel/lumen-framework/src/Application.php The code that initializes the lumen timezone section was found in the file


/**
* Create a new Lumen application instance.
*
* @param string|null $basePath
* @return void
*/
public function __construct($basePath = null)
{
 date_default_timezone_set(env('APP_TIMEZONE', 'UTC'));
 $this->basePath = $basePath;
 $this->bootstrapContainer();
 $this->registerErrorHandling();
}

The. env parameter used in the code is APP_TIMEZONE, and the value is UTC. Change UTC to PRC here, or add it in the. env file

APP_TIMEZONE=PRC

Troubleshooting of lumen php Time Zone Settings.

Summary of Lumen Time Zone Setting

Edit the. env file to add configuration


APP_TIMEZONE=PRC
DB_TIMEZONE=+08:00

If the. env profile is not enabled, edit


/vendor/laravel/lumen-framework/config/database.php
/vendor/laravel/lumen-framework/src/Application.php

Modify the APP_TIMEZONE and DB_TIMEZONE parameter values, respectively.

Enable the. env profile

Rename the.env. example file under the root of Lumen to.env, edit the/bootstrap/app. php, and uncomment the following line of code
Dotenv::load(__DIR__.'/../');

Additional:

Because lumen uses Greenwich Mean Time by default, it needs to be converted to Beijing Time.
Add in. env

APP_TIMEZONE=PRC
DB_TIMEZONE=+08:00

So the time is right


Related articles: