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

EliasSoares's avatar

Laravel validate Time hh:mm

How to validate a hh:mm time with laravel Validator?

Is there any way? Or I will need to use regex validation?

0 likes
4 replies
ghprod's avatar
ghprod
Best Answer
Level 1

Use date_format rule validation

date_format:H:i

From docs

date_format:format

The field under validation must match the format defined according to the date_parse_from_format PHP function.
20 likes
snaveenrpm's avatar

return Validator::make($data, [ 'appointment_date' => 'required|date_format:d-m-Y', 'from_time' => 'required|date_format:H:i', 'to_time' => 'required|date_format:H:i', ]);

This is the correct way to do time validation.

adriiiprieto's avatar
$beginHour = Carbon::parse($request['hour_begin']);
        $endHour = Carbon::parse($request['hour_end']);
        if($beginHour->addMinute()->gt($endHour)){
            return response()->json([
                'message' => 'end hour should be after than begin hour',
            ], 400);
        }

vguerrero's avatar

Take into account, that date_format only validates times in 24 hours format at maximum.

If you have to validate more than 24 hours you have yo use a custom regex validation.

$request->validate([
      'available_hours' => 'regex:/(\d+\:\d+)/',
]);

This rule will accept 33:00 hours, for example.

2 likes

Please or to participate in this conversation.