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

RafaelMunoznl's avatar

Populate a form (input type="date" or type="time") with data from the DB

This should be a simple question, however I do not find the answer anywhere.

I am building a simple Form that should be populated from the Data comming from the Database. The point here is that the data type from the columns from and to is Datetime.

I am using carbon in my application. Therefore I defined this in my Model

    protected $dates = ['from', 'to'];

The form looks like this

<form method="POST" action="{{route('frontend.user.workingtimes.update', $workingtime->id )}}" accept-charset="UTF-8">
	{{ csrf_field() }}
	{{ method_field('PUT') }}
<input name="employee_id" type="hidden" value="{{ $workingtime->employee_id }}">


<label>@lang('labels.frontend.openinghours.from')</label>

<input class="form-control" type="time" value="{{ $workingtime->from->isoFormat('HH:mm') }}" name="from">

<label>@lang('labels.frontend.openinghours.to')</label>	
<input class="form-control" type="time" value="{{ $workingtime->to->isoFormat('HH:mm') }}" name="to">

<button type="submit" class="btn btn-block btn-violeta mt-5">{{ ucfirst(__('labels.general.buttons.update')) }}</button>

</form>

I want to populate the form with the data comming from the DB, but there is not way that the data is shown directly in the input field (populated). I finnd always the answer when the <input type="text" but nowhere for <input type="date" or <input type="time"

Things like these, does not work:

<input type="time" value="{{ $workingtime->from->isoFormat('HH:mm') }}" name="from">

// or

<input type="time" value="{{ $workingtime->from }}" name="from">

How is the correct way?

0 likes
2 replies
Snapey's avatar

For date fields, the value needs to be set in the format YYYY-MM-DD

for time fields, the value needs to be set in the format HH:MM

As you already cast the field to Carbon, you can just use format

<input class="form-control" type="time" value="{{ $workingtime->to->format('H:i') }}" name="to">

a bookmark to this page is useful to keep as it tells you the php specific date/time formatting characters

http://newcircleconsulting.com/2007/11/php-data-format-cheat-sheet/

3 likes

Please or to participate in this conversation.