Validate dates with "now" or "yesterday" words in Laravel
You can validate dates by rules before/after and passing various strings as a parameter, like: "tomorrow", "now", "yesterday". Example: 'start_date' => 'after:now'. It's using strtotime() under the hood.
$rules = [
'start_date' => 'after:tomorrow',
'end_date' => 'after:start_date'
];
Other date validation rules:
date
The field under validation must be a valid, non-relative date according to the strtotime
PHP function.
date_equals:date
The field under validation must be equal to the given date. The dates will be passed into the PHP strtotime
function.
date_format:format
The field under validation must match the given format. You should use either date
or date_format
when validating a field, not both. This validation rule supports all formats supported by PHP's DateTime class.
before:date
The field under validation must be a value preceding the given date. The dates will be passed into the PHP strtotime
function. In addition, like the after
rule, the name of another field under validation may be supplied as the value of date
.
before_or_equal:date
The field under validation must be a value preceding or equal to the given date. The dates will be passed into the PHP strtotime
function. In addition, like the after
rule, the name of another field under validation may be supplied as the value of date
.
after:date
The field under validation must be a value after a given date. The dates will be passed into the strtotime
PHP function:
'start_date' => 'required|date|after:tomorrow'
Instead of passing a date string to be evaluated by strtotime
, you may specify another field to compare against the date:
'finish_date' => 'required|date|after:start_date'
after_or_equal:date
The field under validation must be a value after or equal to the given date. For more information, see the after rule.