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

228
Visualizações
Laravel Carbon does not consider timezone DST

Given my UsersTableSeeder.php class, I am seeding my database with fake data, using a loop:

$numberOfUsers = 150;

DB::table('users')->delete();

$faker = Faker::create();

for ($i = 1; $i <= $numberOfUsers; $i++) {
    DB::table('users')->insert([
        'id' => $i,
        'firstName' => $faker->firstName,
        'lastName' => $faker->lastName,
        'email' => $faker->email,
        'password' => bcrypt("123"),
        'created_at' => Carbon::now()->addDays((-5 * $i) - 2)->format('Y-m-d H:i:s'),
        'updated_at' => Carbon::now()->addDays(-5 * $i)->format('Y-m-d H:i:s'),
    ]);
}

The problem here is that when my datetime values are generated, there is a chance it might fall in a DST zone, like between 2017-03-12 02:00:00 and 2017-03-12 02:59:59 (which does happen) and it gives me the following error:

[PDOException]
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2016-03-13 02:08:11' for column 'created_at' at row 1

Now I understand that I cannot put such a value in my database, because my database is smart enough to know that this zone in time doesn't exactly exit. But is there any way I can 'make' Carbon smart enough to consider DST ? I do not want to manually check it with something like:

if ($my_date > 2017-03-12 02:00:00 && $my_date < 2017-03-12 02:59:59)
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

In Fact Carbon can handle DST zones.

$date = '2017-03-26 02:01:01';
$date = \Carbon\Carbon::parse($date, 'Europe/Berlin');
dd((string)$date);

Results in

// an extra hour was added automatically
"2017-03-26 03:01:01"

Per default Laravel uses 'UTC' for all date and datetime operations (including Carbon). This value can be set in app/config.php

If you actually want to your datetimes be considered in 'UTC' in database a dirty workaround could be something like that:

EDIT:

// calculate current offset to UTC:
$offset = \Carbon\Carbon::now('America/Montreal')->offsetHours;

'created_at' => Carbon::now('America/Montreal')->addHours(-4 + $i)->addDays((-1 * $i) - 2)->tz('UTC')->addHours($offset)->format('Y-m-d H:i:s')
about 4 years ago · Santiago Trujillo Relatório

0

You can generate your timestamps with Carbon and provide the timezone. Try just:

Carbon::now()->format('c')
about 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