Lumen timezone time zone setting method

Source: Internet
Author: User
Tags php mysql
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, this article on the main and everyone to share the lumen timezone time zone setting method, I hope to help everyone.





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'  => 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.phpfind 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($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, 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=PRC
DB_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:



Talking about the custom dependency injection of lumen frame



The system time zone setting for PHP date and time functions



Linux PHP MySQL Time zone Setup method _php Tutorial


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.