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

RafaelMunoznl's avatar

Pack a selected date from the datepicker in a variable and send it to the controlle

I have never worked with datepickers before and I am struggling with something that might be very simple

I am building a calendar and I want to allow users to navigate to any given day chosen in a datepicker.

My code is like that:

The method in the controller:

    public function show(Company $company, $date = null)
    {
        $date = Carbon::parse($date);
    }

The route:

Route::get('{company}/{date?}', 'SalonsController@show')->name('salon.show');   

The form in HTML

<form action="{{route('salon.show', [$company, 'date'])}}" method="GET">
    {{ csrf_field() }}
    {{ method_field('GET') }}
    <label class="input tag form-control">
        <input name="date" type="date" value="date" id="date">
        <button class="btn heute text-center" type="submit">Senden</button>
    </label>
</form>

Let's say I select the Nov. 27, 2019 in the date picker. The I see the URL something like this:

http://project.test/company/date?_token=wlUxABTbnZy6jeAbAwxDVVs9JdxkycUx7GBTx4GE&_method=GET&date=2019-11-27

Note that 'date' is passed in the URL, behind the company, so the routing is working properly sending $company and 'date'.

But I get the following error:

DateTime::__construct(): Failed to parse time string (date) at position 0 (d): The timezone could not be found in the database

It is logic since I'm pasing a string in the form. Here -> <form action="{{route('salon.show', [$company, 'date'])}}" method="GET">

But if I change to a variable $date. That variable does not exist and I get an error.

The question is.

How do I pack the selected date from the datepicker in a variable and send it to the controller?

0 likes
6 replies
Tray2's avatar

Since you are sending it as a GET you can simply do

$date = $request->query('date');
RafaelMunoznl's avatar

@tray2 but 'date' is still a string. I am not getting the date type format from the picker. That is my problem. I am sending a variable named $company and a string: 'date'.

Tray2's avatar

Use Carbon to convert it

Carbon::parse($request->query('date'))->format('Y-m-d');

Eveything you pass on the query string is a string.

Snapey's avatar
Snapey
Best Answer
Level 122

Form fields are always strings. You need to clear up a few things.

Change your form to POST

remove the {{method_field}}

Remove the /{date?}/ from the route since it cannot have any effect

Remove the $date from the controller method signature

change controller to correctly parse the date string in the request

The method in the controller:

    public function show(Request $request, Company $company)
    {
        $date = Carbon::createFromFormat('Y-m-d', $request->date);
    }

The route:

Route::post('{company}', 'SalonsController@show')->name('salon.show');   


The form in HTML

<form action="{{route('salon.show', $company)}}" method="POST">
    {{ csrf_field() }}
    <label class="input tag form-control">
        <input name="date" type="date" value="date" id="date">
        <button class="btn heute text-center" type="submit">Senden</button>
    </label>
</form>
RafaelMunoznl's avatar

@snapey Great! It worked just with a little twick. In order to work the route should be POST, not GET

Route::post('{company}', 'SalonsController@show')->name('salon.show'); 
Tray2's avatar

This is where the date is passed to the request

http://project.test/company/date?_token=wlUxABTbnZy6jeAbAwxDVVs9JdxkycUx7GBTx4GE&_method=GET&date=2019-11-27

This is the query string

?_token=wlUxABTbnZy6jeAbAwxDVVs9JdxkycUx7GBTx4GE&_method=GET&date=2019-11-27

So this is what the date picker passes

date=2019-11-27

Everything passed in the query string is a string and in this case the string of

2019-11-27

To store that in a date field in your database you need to format it and that can be done with Carbon.

Carbon::parse($request->query('date'))->format('Y-m-d');

However like @snapey said convert your form method to POST instead and also all fields in a html form is a string even date and number fields.

Please or to participate in this conversation.