Today with the lumen framework to write code, is also the first experience lumen, encountered a problem, from the database to find the time than the database saved TIMESTAMP time 8 hours, it is clear that this is a time zone setting problem, I thought can be resolved within 1 minutes, but I was wrong
Based on the experience of Laravel 4.x and 5.0, just go to config/app.php to set the ' timezone ' parameter to ' PRC ' and find the Lumen Config directory in/vendor/laravel/lumen -framework/config path, but there is no timezone parameter option in the config/app.php parameter options, manual Plus is not valid.
Then think of the. env file of Laravel 5 and found that there are no options in lumen. env file for timezone settings.
Go back to the Config directory and see 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 ' and env (' Db_ PREFIX ', '), ' timezone ' = env (' Db_timezone ', ' +00:00 '), ' strict ' = False,],
Here is a database of the timezone settings, the default +00:00, that is, UTC time, changed to +08:00 problem resolution. Because the project has the. env configuration file enabled, a line is eventually added to the. env file
DB_TIMEZONE=+08:00
Database timezone problem resolution.
The timezone problem of the database is solved, but the timezone problem of the app has not been solved, the global search Lumen project, find the place of timezone, /vendor/laravel/lumen-framework/src/Application.php
find the code of initializing lumen timezone part in the file
/*** Create A new lumen application instance.** @param string|null $basePath * @return void*/public function __construct ($b Asepath = 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, the value is UTC, where UTC is changed to PRC, or added in an. env file
APP_TIMEZONE=PRC
Lumen PHP time zone setting problem resolved.
Lumen Time Zone Settings summary
Edit. env file Add configuration
app_timezone=prcdb_timezone=+08:00
If the. env configuration file 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 configuration file
Rename the. env.example file under the lumen root directory to. Env, edit/bootstrap/app.php, and uncomment the following line of code
Dotenv::load (__dir__. ' /.. /');
Add:
Because Lumen uses Greenwich time by default, it needs to be converted to Beijing.
Add in. env
App_timezone=prc
db_timezone=+08:00
That's the right time.
Related recommendations:
A brief explanation of the simple implementation method of PHP Nginx real-time output
PHP Comment Syntax specification and naming specification
PHP language comments, single-line comments and multi-line comments related content