Home Backend Development PHP Tutorial Lumen timezone time zone setting method

Lumen timezone time zone setting method

Jun 29, 2018 pm 05:45 PM
lumen Time zone

When I used the Lumen framework to write code today, it was also my first time to experience Lumen. I encountered a problem. The time found from the database was 8 hours slower than the TIMESTAMP time saved in the database. Obviously this is a time zone setting problem. , I thought it could be solved within 1 minute, but I was wrong

According to the experience of Laravel 4.x and 5.0, you only need to set the 'timezone' parameter to 'PRC' in config/app.php Just fine, find Lumen's config directory, under the /vendor/laravel/lumen-framework/config path, but there is no timezone parameter option in the parameter options of config/app.php, and it is invalid even if it is added manually.

Then I thought about the .env file of Laravel 5, and found that there is no option for timezone setting in Lumen’s .env file.

Go back to the config directory and look 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,
],
Copy after login

In There is a database timezone setting here. The default is 00:00, which is UTC time. Changing it to 08:00 solves the problem. Since the project enabled the .env configuration file, a line

DB_TIMEZONE= 08:00

was finally added to the .env file. The database timezone problem was solved.

Although the timezone problem of the database has been solved, the timezone problem of the app has not been solved. Search the lumen project globally to find the place where timezone is used. It is in /vendor/laravel/lumen-framework/src/ The code for initializing the lumen timezone part was found in the Application.php 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();
}
Copy after login

The .env parameter used in the code is APP_TIMEZONE, and the value is UTC. Here, change UTC to PRC, or add

APP_TIMEZONE=PRC

lumen php's time zone setting problem in the .env file.

Summary of Lumen time zone settings

Edit .env file to add configuration

APP_TIMEZONE=PRC
DB_TIMEZONE=+08:00
Copy after login

If it is not enabled .env configuration file, edit

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

to modify the APP_TIMEZONE and DB_TIMEZONE parameter values ​​respectively.

Enable .env configuration file

Rename the .env.example file in the Lumen root directory to .env, edit /bootstrap/app.php, and cancel as follows Comments on the line of code
Dotenv::load(__DIR__.'/../');

Addition:

Because lumen uses Greenwich time by default, it needs to be converted to Beijing time.
Add

APP_TIMEZONE=PRC to .env
DB_TIMEZONE= 08:00

The time will be correct

Related recommendations:

Explanation of the simple implementation method of nginx real-time output

Detailed explanation of PHP comment syntax specifications and naming specifications

php language comments, single-line comments and multi-line comments related content

The above is the detailed content of Lumen timezone time zone setting method. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set the time zone to Japan in php How to set the time zone to Japan in php Mar 20, 2023 pm 04:08 PM

A time zone refers to the standard time used in a region on Earth. There are 24 time zones in the world, from UTC-12 to UTC+14. When traveling between countries with different time zones, you need to adjust your time according to each country's time zone. PHP has some built-in time functions that allow the time in any time zone around the world to be used.

How to use the PHP framework Lumen to develop an efficient message push system and provide timely push services How to use the PHP framework Lumen to develop an efficient message push system and provide timely push services Jun 27, 2023 am 11:43 AM

With the rapid development of mobile Internet and changes in user needs, the message push system has become an indispensable part of modern applications. It can realize instant notification, reminder, promotion, social networking and other functions to provide users and business customers with better services. experience and service. In order to meet this demand, this article will introduce how to use the PHP framework Lumen to develop an efficient message push system to provide timely push services. 1. Introduction to Lumen Lumen is a micro-framework developed by the Laravel framework development team. It is a

Convert Unix timestamp to time format using time.Unix function and set time zone Convert Unix timestamp to time format using time.Unix function and set time zone Jul 25, 2023 pm 12:28 PM

Title: Use the time.Unix function to convert Unix timestamps to time format and set the time zone. In the Go language, we often need to convert Unix timestamps into a readable time format, and often need to consider setting the time zone. This article will introduce how to use the Unix function in the time package to accomplish this task, and demonstrate how to set the time zone. First, we need to understand what Unix timestamp means. A Unix timestamp is an integer in seconds representing the time since January 1, 1970

How to select the time zone for Tencent Conference reservations - How to select the time zone for Tencent Conference reservations How to select the time zone for Tencent Conference reservations - How to select the time zone for Tencent Conference reservations Mar 04, 2024 am 11:04 AM

Friends, do you know how to select the time zone when booking a Tencent meeting? Today, the editor will explain how to select the time zone when booking a Tencent meeting. If you are interested, come and take a look with the editor. I hope it can help you. Step one: Enter the Tencent Meeting APP and click to book a meeting (as shown in the picture). Step 2: Select the meeting type, such as regular meeting, and click Next (as shown in the picture). Step 3: On the meeting reservation page, click on the time zone (as shown in the picture). Step 4: Select the time zone (as shown in the picture). Step 5: If the setting is successful, click Finish (as shown in the picture).

How to change time zone in php How to change time zone in php Nov 24, 2022 am 09:28 AM

How to modify the time zone in PHP: 1. Set the default time zone to East 8 in the configuration file; 2. Set the time zone through the code "date_default_timezone_set('Asia/Shanghai');"; 3. Set "date_default_timezone_set(' in the PHP page header PRC');".

How to use Lumen framework in PHP How to use Lumen framework in PHP Jun 27, 2023 pm 04:49 PM

How to use the Lumen framework in PHP Lumen is a simplified version of the Laravel framework, which focuses on quickly building lightweight microservices and API applications. Lumen is an open source framework created and maintained by Taylor Otwell. It is known for its spectacular speed and performance. In this article, we will introduce how to use Lumen framework in PHP. 1. Install and configure the Lumen framework First, you need to install the composer tool in your system. Comp

What should I do if the time zone modification in Linux does not take effect? What should I do if the time zone modification in Linux does not take effect? Mar 25, 2023 am 10:18 AM

Solution to changing the time zone in Linux does not take effect: 1. Log in to the Linux system and enter the terminal; 2. Copy the configuration file to modify the time zone; 3. View the hardware clock through the "hwclock --show" command, and then set the hardware clock; 4. Pass "vim /etc/timezone Asia/Shanghai" can modify the time zone.

How to change time zone in php.ini file How to change time zone in php.ini file Mar 22, 2023 pm 03:22 PM

PHP is a very popular server-side programming language. When developing web applications using PHP, we sometimes need to set the time zone in PHP. The default time zone of PHP is "UTC (Coordinated Universal Time)", which is not the time zone we want in many cases, so we need to change the time zone setting in the php.ini file. This article will explain how to change the time zone in the php.ini file.

See all articles