I have MySQL and Laravel on my local server, with default configuration and my timezone is GMT+2.
I don't touch anything in the MySQL configuration and I try something on my database :
SELECT CURRENT_TIMESTAMP, UTC_TIMESTAMP;
Results:
CURRENT_TIMESTAMP UTC_TIMESTAMP
2020-06-09 16:34:58 2020-06-09 14:34:58
Now, I want to request this raw in Laravel:
id device_id lat lng alt at created_at updated_at
1 10 10.000000 10.000000 NULL NULL 2020-06-09 16:21:55 2020-06-09 16:21:55
Here is my test using php artisan tinker:
It shows 16:34 in UTC (GMT+0).
Now I try to change the configuration of mysql (in /etc/mysql/mysql.conf.d/mysqld.cnf), and I set this configuration:
default_time_zone='+00:00'
I reboot my server:
sudo service mysql restart
And then I try again the same test than above:
CURRENT_TIMESTAMP UTC_TIMESTAMP
2020-06-09 14:42:46 2020-06-09 14:42:46
Now, the raw shows something different (from 16 to 14 hours):
id device_id lat lng alt at created_at updated_at
1 10 10.000000 10.000000 NULL NULL 2020-06-09 14:21:55 2020-06-09 14:21:55
And in Laravel:
It shows 14:34 in UTC (GMT+0).
I was expecting Laravel/Carbon (or maybe MySQL) to understand that the date is still the same but it looks like it doesn't.
Also, with the configuration of the Test 1, when a column default to CURRENT_TIMESTAMP Laravel understands it as UTC which is wrong because MySQL will initialize it as GMT+2 (in my case).
For example, when I execute a INSERT INTO query on a table (outside of Laravel, from Adminer or PhpMyAdmin) which have a timestamp column DEFAULT to CURRENT_TIMESTAMP, MySQL will insert "18:00" as GMT+2 while Laravel will read "18:00" as GMT+0.
How can I avoid this problem ? Is setting the mysqld parameter as in Test 2 is right ?
Converting the timezone of your dates from your database server to your application server is a bad idea. They should work with the same timezone, because timezone conversions are not 1:1 conversions due to DST changes. Your local/development environment should be as close as possible as your production environments. And the whole tooling (both DB and Laravel) would better to all be in GMT+00:00 (= "UTC" in Laravel config) so it's UTC-standard based, and you will be able to easily reuse/exchange your data (or a part of it) to an other service/user with any other timezone now or later, timezones like "GMT+2" should come only when you want to display a date to a user you know is in this timezone, not for storage.
You need to change the timezone in PHP configuration.
In your php.ini file, change this value - date.timezone
or you can set the timezone in Laravel config.
in your- app/config/app.php - 'timezone' => 'UTC' change this value.