Reputation: 144
I wanted to validate time input formatted as H:i:s
(e.g. "16:35:00") to be greater than current time.
$request->validate([
'start_time' => 'required|after:now',
'end_time' => 'required|after:start_time',
]);
Above code checks the date instead of time.
Upvotes: 6
Views: 4328
Reputation: 5056
Change the date to be compared to the actual input using setDateFrom
(start in start, end in end) so only time remains checked with what you initially gives as a datetime:
$request->validate([
'start_time' => 'required|after:' . Carbon::now()->format('H:i:s'),
'end_time' => 'required|after:start_time',
]);
Upvotes: 4
Reputation: 2834
However, it won't validate times from different days (eg 09:00 PM - 03:00 AM
the next day). start_time
and end_time
, in this case, should be provided as HH:mm
but you can change it easily.
$this->validate($request, [
'start_time' => 'date_format:H:i',
'end_time' => 'date_format:H:i|after:start_time',
]);
Upvotes: 0