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

phpMick's avatar

Can't set dates

Hi,

I have a date field:

{!! Form::date('launch_date', \Carbon\Carbon::now(), ['class' => 'form-control']) !!}

Where the user can edit the launch_date. In my DB, they are currently null. When I debug I get:

$product->launch_date
Carbon\Carbon::__set_state(array(
   'date' => '-0001-11-30 00:00:00.000000',
   'timezone_type' => 3,
   'timezone' => 'UTC',
))

How on Earth can I pull these from the DB and display then without getting?:

The specified value "Invalid date" does not conform to the required format, "yyyy-MM-dd".

Why do we have to use dates? Throw away your watches and calendars and use Unix time.

Mick

0 likes
6 replies
stevenobird's avatar

The date field (which is deprecated and not a standard anymore according to W3C) takes the date in the format you wrote: "yyyy-MM-dd". Carbon::now() outputs a full datetime value, like "2016-03-23 16:52:00.000".

You can use

Carbon::now()->toDateString();

which will output "2016-03-23".

But since you use a datetime field in your database, you'll need to use a datetime-local field.

phpMick's avatar

When the launch date is null, I get:

$product->launch_date->toDateString(); -0001-11-30

Where does that come from?!

stevenobird's avatar

This is indeed strange.

I tried to create empty/null instances of Carbon, but it always gave me correct dates:

$date = null
$dt = Carbon::parse($date);

Carbon\Carbon {#717
    +"date": "2016-03-24 09:52:01.000000",
    +"timezone_type": 3,
    +"timezone": "Europe/Berlin",
}

$date2 = ''
$dt2 = Carbon::parse($date2);
Carbon\Carbon {#719
    +"date": "2016-03-24 09:53:23.000000",
    +"timezone_type": 3,
    +"timezone": "Europe/Berlin",
}

What is the output of $product->launch_date without some Carbon/Date method? I guess they won't be null, because if they were, you would get a FatalErrorException:

Call to a member function toDateString() on null
stevenobird's avatar

The thread states that it is a PHP issue.

But your invalid date must come from somewhere - when your launch_date is '0000-00-00 00:00:00', you definetly did something wrong - either in database design or in your inserts.

And to be honest, the lack of information you provided (where does the data come from, ...) makes it hard to find the problem's issue.

phpMick's avatar

It comes from here:

Schema::table('products', function($table)
{
    $table->timestamp('launch_date');
});

I get -0001-11-30 when the field is null. I explained this in my first post.

I have now set the field to allow nulls, this should prevent MySQl sticking an actual date in.

I still get the same error, when I try and turn the collection into an array.

Please or to participate in this conversation.