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

madsem's avatar

1292 Incorrect datetime value with protected $dates & faker

I'm writing seeders, and I have one model with $timestamps = false. On that model I have a custom timestamp field, and I cast it using the $dates = [] property.

Now in my seeder I have:

'date' => $faker->dateTimeBetween('-6 months', 'now')->format('Y-m-d'),

But I always get an error:

Invalid datetime format: 1292 Incorrect datetime value: '2020-03-07T08:59:07.000000Z' for column 'date' at row 1

Tried a shitload of things, but nothing works, only when I comment out the

protected $dates = [
        'date',
    ];

the timestamp field saves...

What am I doing wrong? Laravel's documentation says that $dates can be passed in various formats, such as a simple date string like 2019-12-23, but it does not work.

0 likes
4 replies
MichalOravec's avatar

Try

'date' => $faker->dateTimeBetween('-6 months', 'now')->format('Y-m-d H:i:s'),
madsem's avatar

Sorry, I meant I have a custom date field...

When i tinker it, I get a proper date string:

>>> $faker->dateTimeBetween('-6 months', 'now')->format('Y-m-d')
=> "2019-12-23"
>>> 

But whenever I try to seed I get this error.

migration:

$table->date('date');

model:

/**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = [
        'date',
    ];
vlauciani's avatar

Same problem... did you solve? I'm using Laravel 8.

1 like
DreadfulCthulhu's avatar

Same in Laravel 11. Casted as date, but it is still trying to instert into database in ISO format. Any suggestions what is wrong? I know I can make own setter, but why when there is cast? It should get also Carbon object, but it is doing the same as with Y-m-d string

Please or to participate in this conversation.