haakym
haakym

Reputation: 12358

Laravel - after date validation with date from rules set

I'm trying to validate two dates to ensure one is greater than the other using the after validate rule. I have my rules set like this:

$rules = array(
 'date_from' => 'required|date_format:"d/m/Y"', 
 'date_to'   => 'required|date_format:"d/m/Y"|after:date_from',
 // more rules ...
);

When using the following values: date_from = "01/06/2014" and date_to = "01/06/2014" OR date_to = "12/06/2014" everything is hunky dory, however.. using anything above 12 for day fails i.e. date_to = "13/06/2014" to date_to = "31/06/2014"

I've also tried this and it gives the same results:

$dateFromForm = Input::get('date_from');

$rules = array(
 'date_from'  => 'required|date_format:"d/m/Y"', 
 'date_to'    => 'required|date_format:"d/m/Y"|after:' . $dateFromForm,
);

Quite clearly to me it's reading the day as the month, any ideas what I'm doing wrong here?

Thanks

Upvotes: 6

Views: 9326

Answers (1)

Laurence
Laurence

Reputation: 60058

If you look at the Laravel docs - it says

The dates will be passed into the PHP strtotime function.

The problem with strtotime is that it assumes you are using m/d/Y. If you want d/m/Y - you need to change it to d-m-Y to be correctly parsed.

So change your date format to d-m-Y and it will work.

Upvotes: 7

Related Questions