How to validate time in laravel How to validate time in laravel laravel laravel

How to validate time in laravel


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.


Probably this code would work in your controller. However it won't validate times from different days (eg 9pm - 3am next day). time_start and time_end in this case should be provided as HH:mm but you can change it easily.

public function store(Illuminate\Http\Request $request){    $this->validate($request, [        'time_start' => 'date_format:H:i',        'time_end' => 'date_format:H:i|after:time_start',    ]);    // do other stuff}


Create DateRequest and then add

<?phpnamespace App\Http\Requests\Date;use App\Http\Requests\FormRequest;class DateRequest extends FormRequest{    /**     * --------------------------------------------------     * Determine if the user is authorized to make this request.     * --------------------------------------------------     * @return bool     * --------------------------------------------------     */    public function authorize(): bool    {        return true;    }    /**     * --------------------------------------------------     * Get the validation rules that apply to the request.     * --------------------------------------------------     * @return array     * --------------------------------------------------     */    public function rules(): array    {        return [            'start_date' => 'nullable|date|date_format:H:i A',            'end_date' => 'nullable|date|after_or_equal:start_date|date_format:H:i A'        ];    }}