Skip to content

Instantly share code, notes, and snippets.

@devinsays
Created July 26, 2018 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinsays/37b863badf50dc7b77a77a501f0b21f3 to your computer and use it in GitHub Desktop.
Save devinsays/37b863badf50dc7b77a77a501f0b21f3 to your computer and use it in GitHub Desktop.
Resolving "Incorrect datetime value" in Laravel

While importing data into a local MySQL database in Laravel I hit this issue:

PDOException::("SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2018-03-11 02:04:15' for column 'created_at' at row 825")

The problem is due to daylight savings time (DST). In OSX MySQL is generally set to use SYSTEM time rather than UTC.

So, in my case, the issue was that datetime 2018-03-11 02:04:15 technically did not exist in CST.

One suggested solution is to update my.conf to use timezone = UTC, but I could not track down a my.conf file to use or modify (MySQL had been installed with Brew).

But Laravel offers another option. In config/database.php, just add this line to your mysql driver:

'timezone'  => '+00:00'

The full text should look something like this:

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
    'timezone'  => '+00:00'
],

That seemed the easiest and worked for me.

@ejntaylor
Copy link

Mega thanks for sharing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment