Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

452
Views
Laravel and MySQL timestamp value changes with mysqld configuration

I have MySQL and Laravel on my local server, with default configuration and my timezone is GMT+2.

Test 1

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:

TEST 1

It shows 16:34 in UTC (GMT+0).

Test 2

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:

enter image description here

It shows 14:34 in UTC (GMT+0).

The problem

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 ?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!