Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

453
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda