Check date in laravel 5

I have a StartDate field and an EndDate field. Both can begin today or in the future. Perhaps they will begin on the same day, including today. I have to do validation for them. What i have done so far:

'StartDate'=>'required|date_format:Y/m/d|after:yesterday', 'EndDate' => 'date_format:Y/m/d|after:yesterday', 

What I do not know how to do is to check if both dates are equal to the second condition.

Can anyone help me please?

+7
source share
2 answers

EDIT This was a solution prior to Laravel 5.3. In Laravel 5.4, the after_or_equal rule was introduced

https://laravel.com/docs/5.4/validation#rule-after-or-equal


I had the same need for my web application. I solved this by creating my own Request using the following rules method:

 public function rules() { $rules = [ 'start_date' => 'date_format:Ymd|after:today' ]; if ($this->request->has('start_date') && $this->request->get('start_date') != $this->request->get('end_date')) { $rules['end_date'] = 'date_format:Ymd|after:start_date'; } else { $rules['end_date'] = 'date_format:Ymd|after:today'; } return $rules; } 
+8
source

As I understand it, I'm trying to prepare a special verification rule that can help you fulfill your requirements.

The current rule mentioned below only checks the start date before the end date.

 <?php namespace App\Providers; use Validator; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider{ public function boot(){ Validator::extend('date_after', function($attribute, $value, $parameters) { return strtotime( $value ) > strtotime( $this->attributes[ $parameters[0] ] ); }); Validator::extend('date_equal', function($attribute, $value, $parameters) { return strtotime( $value ) == strtotime( $this->attributes[ $parameters[0] ] ); }); } } ?> 

Using:

 <?php $inputs = array( 'start_date' => '2013-01-20', 'end_date' => '2013-01-15' ); $rules = array( 'start_date' => 'required', 'end_date' => 'required|date_after:start_date' ); $messages = array( 'date_after' => ":attribute must be date after Start Date." ); $validation = Validator::make($inputs, $rules, $messages); if( $validation->fails() ) { echo '<pre>'; print_r($validation->errors); echo '</pre>'; } ?> 

However, I have not tested this code with my env ... but I believe that it helps you deal with the requirements.

Please let me know your further requirement so that I can provide you with a specific solution to your requirement.

0
source

All Articles