This is because you do not deliver the day, so the current day is used by default. The current day is 31, but September has only 30 days, so it skips October 1.
Take a look at this example:
function validateDate($date, $format = 'm-Y') {
$d = DateTime::createFromFormat($format, $date);
echo $d->format("d-".$format);
return $d && $d->format($format) == $date;
}
var_dump(validateDate('08-2017', 'm-Y'));
var_dump(validateDate('09-2017', 'm-Y'));
Functionwas copied from this or php.net
This is a bit rudimentary, but you can determine if dthe format is there and manually set it to 1 to avoid this:
<?php
function validateDate($date, $format = 'm-Y') {
if (strpos($format, "d") === false) {
$format = "d ".$format;
$date = "01 ".$date;
}
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) === $date;
}
var_dump(validateDate('08-2017', 'm-Y'));
var_dump(validateDate('09-2017', 'm-Y'));
source
share