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

christopher's avatar

Save Datepicker Date field to database

I am struggeling since hours :)

I create a date with the bootstrap datepicker. I changed the format to the european date format for the picker d.m.yyyy.

Now of course i cant save this date to the mysql database date type column.

In my Controller i have

// $delivery_date is 31.12.2015
Carbon::createFromFormat('d.m.yyyy', $request->input('delivery_date'))->format('Y-m-d');

But i am always getting

InvalidArgumentException in Carbon.php line 425:
Data missing

Does someone know what i do wrong? Which data is missing?

0 likes
8 replies
fraserk's avatar

I I believe the input('delivery_date') is not a carbon instance you cannot use ->format

try in your controller

$date = Carbon::createFromFormat('Y-m-d', $request->input('delivery_date'));
1 like
christopher's avatar

@fraserk

No its just my input field from the datepicker form. With your suggestion i get also an error

Unexpected data found.
Unexpected data found.
Trailing data

mhhhh....

fraserk's avatar
fraserk
Best Answer
Level 16

That means Carbon is not getting the expected date format. Are you sure you're passing in a correct date format?

1 like
christopher's avatar

@fraserk My datepicker submits the following date format: 31.12.2015

So now i try to replace the point with a dash

$date = str_replace('.', '-',$request->input('delivery_date'));

The output is "25-12-2015". ( d-m-yyyy ).

If i now try Carbon::createFromFormat('d-m-yyyy', $date)->format('Y-m-d'); i still get the same error message. If i try your code Carbon::createFromFormat('Y-m-d', $date); i get Trailing data

fraserk's avatar

try

Carbon::createFromFormat('Y-m-d H:i:s', $date));

christopher's avatar

@fraserk

Okay this is now working:

Form Input: 31.12.2015 ( d.m.Y )

Controller:

// replace the point from the european date format with a dash
$date = str_replace('.', '-', $request->input('delivery_date'));
// create the mysql date format
$carbon->createFromFormat('d-m-Y', $date)->toDateString();

Output: correct date format -> "2015-12-31"

1 like
simioluwatomi's avatar

Sorry for unearthing an old question but I had this challenge today and solved it like this

$date_variable = Carbon::createFromFormat('d-m-Y', $request->input('date'))->format('Y-m-d');

The first argument should be the date format that the datepicker is submitting; the second argument is the raw input the datepicker submits. The last argument is the desired format you want Carbon to format the date to.

Hope this helps someone out there.

1 like

Please or to participate in this conversation.