Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 416
haakym Avatar asked Jun 05 '14 14:06

haakym


1 Answers

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.

like image 85
Laurence Avatar answered Oct 11 '22 00:10

Laurence