Jump to content

"date" sanitizer failing on Feb 31


tpr
 Share

Recommended Posts

I just noticed these outputs:

echo wire('sanitizer')->date('2017/02/27'); // 1488153600
echo wire('sanitizer')->date('2017/02/31'); // 1488499200 - FAIL
echo wire('sanitizer')->date('2017/02/32'); // NULL

Shouldn't the sanitizer return NULL on the second line? If it were looking for a valid date format only then the third line should return a timestamp too.

Link to comment
Share on other sites

From the php docs:

Quote

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.

If you really want to be sure about date parsing you'd need to specify the format otherwise you'll always find issues.

Link to comment
Share on other sites

Thanks, but even the format supplied the date is not null:

echo wire('sanitizer')->date('2017/02/31', 'Y/m/d'); // 2017/03/03
echo wire('sanitizer')->date('2017-02-31', 'Y-m-d'); // 2017/03/03

 

Link to comment
Share on other sites

Looks like internally $sanitizer->date() is relying on strtotime() to check that the date is valid, and this function for some reason returns a timestamp for '2017/02/31' rather than 'false'.  It should perhaps be changed to checkdate() which correctly identifies that date as invalid.

In the meantime you could add an extra validation:

function validate_date($value) {
    $parsed = date_parse($value);
    return checkdate($parsed['month'], $parsed['day'], $parsed['year']);
}

 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...