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

Sema314's avatar

Laravel returning -0001-11-30 on API response if DB is 0000-00-00

HI all,

So I wanted to double check and see if anyone has come across this issue before and how might I be able to resolve it?

So inside the DB, I get a "Empty" return shown as "0000-00-00" which is this snippet, it's its empty or null, it'll write "0000-00-00" in the DB, otherwise it'll have the date:

    public function mapObject($obj)
    {
        return [
            'birthday' => empty($obj->birthday) ?
                '' :
                Carbon::parse($obj->birthday)->format('Y-m-d'),
            'hire_date' => empty($obj->hire_date) ?
                '' :
                Carbon::parse($obj->hire_date)->format('Y-m-d'),
        ];
    }

This is my API function:

    public function transform($employee)
    {
        return [
            'birthday' => $employee['birthday']->format('Y-m-d'),
            'hire_date' => $employee['hire_date']->format('Y-m-d'),
        ];
    }

Whenever the DB has "0000-00-00" as the input (null/empty), it's returning the fields as "-0001-11-30" through the API call above.

0 likes
2 replies
bobbybouwmann's avatar

Well, this happens because 0000-00-00 is not a valid date range and therefore it gets parsed into an incorrect date.

I would advise you to make the column nullable in your database. So instead of storing 0000-00-00 in the database, I would store NULL.

If that is not an option, you can always check on the value

'birthday' => $employee['birthday'] === '0000-00-00' 
    ? 'No valid date available' 
    : $employee['birthday']->format('Y-m-d'),
Sema314's avatar

Thanks so much man!

I like the NULL approach better,

So by default, now I'm storing NULL on both fields by supplying the following in the migration file:

            $table->date('birthday')->nullable('0000-00-00')->after('email');
            $table->date('hire_date')->nullable('0000-00-00')->after('birthday');

The problem that I'm running into now, is when the data get imported, the empty/null fields get converted to todays date - Do you know what might cause this? So it's showing "2020-07-01" on the null fields.

Please or to participate in this conversation.