5
function validateDate($date, $format = 'm-Y') {
    $d = DateTime::createFromFormat($format, $date);
    return $d && $d->format($format) == $date;
}


validateDate('09-2017', 'm-Y');

function was copied from this answer or php.net

I'm very puzzled to why this returns false while it returns true for the previous months. Any ideas?

2 Answers 2

6

It's because you're not supplying a day, so it's using the current day by default. Current day is 31, but September only has 30 days, so it skips to October 1st.

Look at this example:

function validateDate($date, $format = 'm-Y') {
    $d = DateTime::createFromFormat($format, $date);
    echo $d->format("d-".$format); // added the day for debugging
    return $d && $d->format($format) == $date;
}
var_dump(validateDate('08-2017', 'm-Y')); // 31-08-2017, true
var_dump(validateDate('09-2017', 'm-Y')); // 01-10-2017, there's no 31-09-2017, false

function was copied from this answer or php.net

This is a little rudimentary, but you can detect if there's no d in the format 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')); // 31-08-2017, true
var_dump(validateDate('09-2017', 'm-Y')); // 01-09-2017, true
Sign up to request clarification or add additional context in comments.

3 Comments

Ahhhhh! Nice catch! Excellent explanation. Thank you very much! :-)
It's no problem! I added a possible solution I didn't like at all, but check it out!
Beautiful! I love the quick lookup for "d" within the format. Seems like the most efficient way to do it.
1

If you are not supplying a day. current one is used; just like explained in previous answer. But this can be quickly solved with one format sign: !, which resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to the Unix Epoch; see format table.

Fix:

function validateDate($date, $format = 'm-Y') {
    $d = DateTime::createFromFormat('!'.$format, $date);
    return $d && $d->format($format) == $date;
}

function was copied from this answer or php.net

1 Comment

How was my function copied? It’s two lines of code to do a typical date check, and not even complicated lines. I even made a mistake lol. Great answer though, didn’t know about !, +1 Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.