Categories
Code

Laravel UK date validator

Extend Laravel’s validator class to fully validate dates in the format (DD/MM/YY). Tested in laravel 3.

public function validate_uk_date($attribute, $value, $parameters)
{
/*
Validates a UK date to fit DD/MM/YY, checking for days in the month and accounting for leap years.
Laravel's date validator didn't seem to do that.
*/

//split date into array of parts
$date_array = strptime ( $value, '%d/%m/%y' );
//return early if that failed
if ( ! $date_array ) return false;
//check for validity of date
return checkdate( (1+$date_array['tm_mon']), $date_array['tm_mday'], (1900+$date_array['tm_year']) );
}