Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ahoi's avatar
Level 5

Incorrect datetime value: '2021-03-28 02:44:56'

Hello everybody,

from time to time I am getting this error in my feature tests:

  SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2021-03-28 02:44:56' for column `laravel_test`.`products`.`created_at` at row 1 (SQL: insert into `products ` (`stock`, `created_at`) values (1, 2021-03-28 02:44:56))

The migration for this col looks like this:

$table->timestamps();

I am running this MariaDB version:

Server version: 10.6.4-MariaDB Homebrew

Is it correct, that the only way to manage this is to set strict to false in config/database.php:

'mysql' => [
    'driver' => 'mysql',
    // ...
    'strict' => false,
   // ...
],

I'd like to hear your opinions about that :-)

0 likes
9 replies
Snapey's avatar

wonder why it is using that format and not '2021/03/28'

Are you doing something with locale

1 like
ahoi's avatar
Level 5

@Snapey

In my .env there's something:

APP_LOCALE=en, but that seems to be only used to stuff it into a mix-variable:

MIX_LOCALE="${APP_LOCALE}"

In config/app.php there is

'locale' => 'en',
'fallback_locale' => 'en',

But within the User-model there is a preferred locale set:

public function preferredLocale(): mixed
{
    return $this->country ? $this->country : 'en';
}

Tinkering around, this is the output of app()->getLocale():

=> "en"
ahoi's avatar
Level 5

@Snapey

I just checked, how SELECT NOW(); behaves in MariaDB:

SELECT NOW();

> 2021-11-28 12:55:39

So it seems this is the correct format ( @tray2 stated this in another reply in this conversation).

ahoi's avatar
Level 5

@Snapey

But the correct thing is that the value I passed is not a string like it's supposed to be.

Tray2's avatar
Tray2
Best Answer
Level 73

MariaDB and MySQL use the same date format YYYY-MM-DD HH:MM:SS.ffffff

The timestamp should be sent in as a string

INSERT INTO t  VALUES (4, '2001-07-22 12:12:12');
1 like
ahoi's avatar
Level 5

@Tray2

I wondered about that, too. I just saw that this model has no 'datetime'-casting for created_at - I will dig a bit deeper to see, if this could be the reason.

Tray2's avatar

@ahoi Share the code that does this and we might be able to spot the issue.

ahoi's avatar
Level 5

@Tray2

Sure:

class ProductFactory extends Factory
{

    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Product::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'stock' => $this->faker->numberBetween(1, 5),
            'created_at' => $this->faker->dateTimeBetween(now()->subYear(), now()),
        ];
    }
}

Adding a custom created_at-value is important for my database-seeding here, as I am collecting some metrics like "how many products did you have over the years?".

But it seems like this custom created_at-state is causing the trouble here.

ahoi's avatar
Level 5

Should be

$this->faker->dateTimeBetween(now()->subYear(),now())->format('Y-m-d H:i:s')

Please or to participate in this conversation.