Search code examples
phplaraveldatedatetimelaravel-livewire

Laravel / Livewire - Date is off by one day


I'm not sure where this comes from but the default HTML datepicker is off by one day while using laravel and livewire.

This code:

<div class="col-span-6 sm:col-span-4">
    <x-jet-label for="birthdate" value="{{ __('Geburtsdatum') }}" />
    <x-jet-input id="birthdate" name="birthdate" type="date" class="mt-1 mb-6 w-full" wire:model="details.birthdate"/>
    <x-jet-input-error for="details.birthdate" class="mt-2" />

    {{$details->birthdate}}
</div>

ends up like this:

datepicker off by one day

I use this code to ensure that the birthday format in my model is the way that I expect it to be:

protected $casts = [
    'birthdate' => 'date:Y-m-d',
];

The same with the validation rules in my livewire component:

protected $rules = [
    'details.birthdate' => 'date:Y-m-d',
];

Also i use the date format in my mysql database.

And the value seems to be right as 2021-02-02 but inserting this using wire:model it ends up as 2021-02-01.

Does someone know where this behaviour comes from?


Solution

  • Finally we found what was causing the problem. We had a protected variable in our model:

    protected $dates = ['birthdate'];
    

    After deleting this, if finally worked. Thanks for your help.