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

227
Views
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 answers
Answer question

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 Report

0

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

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