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'
Related videos on Youtube
Author by
Success Man
Updated on June 04, 2022Comments
-
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 over 6 yearsYou most likely need to write a custom validation rule.
-
fubar over 6 yearsCustom validation rules docs. You try first and come back when you have questions.
-
-
fubar over 6 yearsSurely you need to use the
request_date
value, rather than the current time? -
gview over 6 yearsHe 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 over 6 years@gview, I try this :
'request_date' => 'required|date_format:Y-m-d H:i:s|after:5 hours'
, it works -
victorf over 5 yearsstill valid for laravel 5.7