How to validate time in laravel

56,779

Solution 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.

Solution 2

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
}

Solution 3

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'
        ];
    }
}

Solution 4

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());
}

enter image description here

Solution 5

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.'
        ];

    }

}
Share:
56,779

Related videos on Youtube

Vikash
Author by

Vikash

Passionate programmer, Developer, Learner. I am also here to contribute to this community with my answers which might be useful to others.

Updated on May 28, 2020

Comments

  • Vikash
    Vikash almost 4 years

    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

  • Vikash
    Vikash over 7 years
    That I know, but I want to know the logic how to validate this time
  • Umbert P.
    Umbert P. over 7 years
    Take a look to this: 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);
  • ahinkle
    ahinkle about 6 years
    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',
  • Farid
    Farid over 3 years
    Just as a comment, according to laravel docs "You should use either date or date_format when validating a field, not both"