Home > Blockchain >  Lumen date timezone from database
Lumen date timezone from database

Time:01-06

I change my timezone from UTC to Europe/Paris in my env file.

When I create a DateTime, the time is correct (from Paris).

$now = new \DateTime();
echo $now->format('Y-m-d H:i:s'); // correct

Now, in my databse (MySQL) I have a Timestamp field and I get time -1h :

// database value = 2022-01-31 13:35:42
$expired = new \DateTime($this->expired_at);
echo $expired->format('Y-m-d H:i:s'); // show 2022-01-31 12:35:42

Why date get from database look like in UTC instead my new timezone ?

In my PHP project I do :

SET @@global.time_zone = ' 01:00';
SELECT @@global.time_zone, now();

When Paris time is 14:00 in PHP for the last query I get :

@@global.time_zone =>  01:00 
now() => 13:00 // instead of 14:00

CodePudding user response:

MySQL timezone is separated from PHP timezone. They are two different applications. To set MySQL timezone run this MySQL query in your PHP code:

SET @@global.time_zone = ' 01:00';

To make sure that works true run this query:

SELECT @@global.time_zone, now();

Edit based on comments

As told in the comments Laravel framework is used. So the following post is suggested: Is there any way I can set timezone in laravel 4 database.php configuration file?

Go to app->config->app.php in your Laravel directory. On line 43 you can >alter the default timezone to whatever you need.

'timezone' => 'Europe/Paris',
  •  Tags:  
  • Related