How can I set comparison datetime in validation laravel?

12,603

I think you can use after:date

The field under validation must be a value after a given date. The dates will be passed into the PHP strtotime function.

You simply need to create the right date string using something like this:

'request_date'=>'after:'.date(DATE_ATOM, time() + (5 * 60 * 60)),

Seems that you can also chain these rules like this:

'request_date' => 'required|date_format:Y-m-d H:i:s|after:5 hours'
Share:
12,603

Related videos on Youtube

Success Man
Author by

Success Man

Updated on June 04, 2022

Comments

  • Success Man
    Success Man almost 2 years

    I use laravel 5.3

    My laravel validation like this :

    <?php
    namespace App\Http\Requests;
    use Illuminate\Foundation\Http\FormRequest;
    class AddCartRequest extends FormRequest
    {
        public function rules()
        {
            return [
                'id'=>'required|numeric',
                ...
                'request_date'=>'required'
            ];
        }
    }
    

    If value of request_date is

    01-08-2017 13:00:00

    I want to add condition like this :

    If request date < now + 5 hour, then it will display message : "Request date must be larger 5 hours from now"

    How can I do it?

    • fubar
      fubar over 6 years
      You most likely need to write a custom validation rule.
    • fubar
      fubar over 6 years
      Custom validation rules docs. You try first and come back when you have questions.
  • fubar
    fubar over 6 years
    Surely you need to use the request_date value, rather than the current time?
  • gview
    gview over 6 years
    He asked for '< now + 5 hour'. It's a validation of an entered date. This validation will fail, if the entered date is not at least 5 hours from now. This is what is being asked for.
  • Success Man
    Success Man over 6 years
    @gview, I try this : 'request_date' => 'required|date_format:Y-m-d H:i:s|after:5 hours', it works
  • victorf
    victorf over 5 years
    still valid for laravel 5.7