I want to validate time in Laravel. Ex:- I want that when user input the time between 8 PM to 10 PM then it will show the validation error. How can I achieve that in Laravel
Asked
Active
Viewed 7.9k times
53
-
You need to use `rule` in that. here is the example you can follow. [https://laracasts.com/discuss/channels/laravel/laravel-validation-for-time-formats] – Manish Sep 13 '16 at 10:26
-
You may also use @Umbert P. link. This also may helps you to validate time period. here is another link [http://stackoverflow.com/questions/33685161/laravel5-1-validate-time-field-start-time-should-be-greater-than-end-time] – Manish Sep 13 '16 at 10:35
6 Answers
97
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.

Chathuranga Tennakoon
- 2,059
- 1
- 18
- 20
86
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
}

Skysplit
- 1,875
- 12
- 16
-
Not working on Laravel 5.6. with the included seconds on one form. ```'time_start' => 'date_format:H:i:s', 'time_end' => 'date_format:H:i|after:time_start',``` – ahinkle Mar 23 '18 at 20:29
-
1
4
Create DateRequest and then add
<?php
namespace 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'
];
}
}

Anil Stha
- 515
- 7
- 12
-
2Just as a comment, according to laravel docs "You should use either date or date_format when validating a field, not both" – Farid Dec 29 '20 at 12:26
-
Link to the quote in the documentation: https://laravel.com/docs/master/validation#rule-date-format – Robin Bastiaan Sep 26 '22 at 12:36
1
Try This code
use Validator;
use Carbon\Carbon;
$timeHours = "7:00 PM";//change it to 8:00 PM,9:00 PM,10:00 PM it works
$time = Carbon::parse($timeHours)->format('H');
$request['time'] = $time;
$validator = Validator::make($request->all(), [
'time' => ['required','integer','between:20,22']
]);
if ($validator->fails()) {
dd($validator->errors());
}

Parithiban
- 1,656
- 11
- 16
1
In Lavavel 5.6:
Inside the file located in /app/Http/Requests/YourRequestValidation
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class YourRequestValidation extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'initial_time' => 'required',
'end_time' => 'required',
'end_time' => 'after:initial_time'
];
}
/**
* Custom message for validation
*
* @return array
*/
public function messages()
{
return [
'initial_time.required' => 'Please, fill in the initial time',
'end_time.required' => 'Please, fill in the end time.',
'end_time.after' => 'The end time must be greater than initial time.'
];
}
}

Alexandre Ribeiro
- 1,384
- 1
- 13
- 19
0
You probably should take a look to this bit of the documentations. A custom rule sounds like your way to go.

Umbert P.
- 187
- 11
-
-
Take a look to this: http://carbon.nesbot.com/ You can then work with times easyly. for example: $dt = Carbon::parse('2012-9-5 23:26:11.123789'); var_dump($dt->hour); – Umbert P. Sep 13 '16 at 10:31