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

muuucho's avatar
Level 11

Validate time H:i

Form:

<input
                                    type="time"
                                    name="slot_duration"
                                    value="{{ old('slot_duration', $calendar->slot_duration ?? '') }}"
                                    id="slot_duration"
                                    class="block mt-1 w-full"
                                >
                                       	

Valudation:

'slot_duration' => ['nullable', 'date_format:H:i'],

In db I save the value as time (H:i:s). When I update the form without changing this field, i get error That format is not H:i, probably due to that 'old' is with seconds. Workaround?

0 likes
2 replies
LaryAI's avatar
Level 58

To address the issue you're facing, the problem arises because the old() function is likely retrieving the time value from the database in the H:i:s format, which includes seconds. However, your validation rule expects the format to be H:i, which does not include seconds.

To solve this, you can format the time value to H:i before passing it to the old() function. You can achieve this by using PHP's Carbon library or the date function to format the time correctly.

Here's how you can modify your code:

  1. Format the time value before passing it to the old() function:

    If you're using the Carbon library, you can do something like this:

    use Carbon\Carbon;
    
    $slotDuration = $calendar->slot_duration ? Carbon::createFromFormat('H:i:s', $calendar->slot_duration)->format('H:i') : '';
    

    If you prefer using PHP's native date function:

    $slotDuration = $calendar->slot_duration ? date('H:i', strtotime($calendar->slot_duration)) : '';
    
  2. Update the value attribute in your input field:

    <input
        type="time"
        name="slot_duration"
        value="{{ old('slot_duration', $slotDuration) }}"
        id="slot_duration"
        class="block mt-1 w-full"
    >
    

By formatting the time value to H:i before it is passed to the old() function, you ensure that the validation rule matches the expected format, thus preventing the validation error.

martinbean's avatar
Level 80

@muuucho When you set the value attribute of the time input, you’ll need to match the input (H:i) again:

<input
    name="slot_duration"
    type="time"
    value="{{ old('slot_duration', optional($calendar->slot_duration)->format('H:i')) }}"
>

I personally create additional methods on my models in order to get values like dates and times specifically for form inputs:

public function formSlotDurationAttribute()
{
    return optional($this->slot_duration)->format('H:i);
}

I can then use this method to populate input values:

<input
    name="slot_duration"
    type="time"
    value="{{ old('slot_duration', $calendar->formSlotDurationAttribute()) }}"
>
1 like

Please or to participate in this conversation.