2

So, a user can input a birth date by typing it or using a date picker. This could allow mistakes, so on the server side I'd like to check if it's a valid date.

I read a lot on strtotime() and saw a lot of examples, but not a 100% correct one.

This is what I have:

$geboortedatum = $_POST['geboortedatum'];
$geboortedatum = date("Y-m-d", strtotime($geboortedatum));
list($y, $m, $d) = explode("-", $geboortedatum);

// var_dump(checkdate($d,$m,$y));
if(checkdate($d, $m, $y)) {
    echo "OK Date";
}
else {
    echo "BAD Date";
    exit();
}

This works most of the time. Is there any solid method to check if the input is a real and correct date?

// EDIT

That was not a good example! It also doesn't work when the user inputs 31-31-1980. This is also valid, although it shouldn't be!

5
  • strtotime is good, but it's not psychic. do NOT depend on it to validate abitrary input formats. It WILL screw up sooner or later. Commented Jul 10, 2013 at 18:02
  • Ok, sure, but how can I check the correct input then? There must be a thing I can do to check if it's correct... Commented Jul 10, 2013 at 18:11
  • well, what's 01-02-03? Feb 1st, 2003? Jan 3rd, 2002? enforce a date format, and if what the user enters doesn't confirm, slap an error message in their face. Commented Jul 10, 2013 at 18:12
  • The date format is dd-mm-yyyy. Just as listed in the 2nd line of the code! When you input 31-02-1980 (dd-mm-yyyy) it is being accepted! Just want to figure out how to check if this is valid or not... Commented Jul 10, 2013 at 18:20
  • similar stackoverflow.com/questions/19271381/… or from manual php.net/manual/en/function.checkdate.php#113205 Commented May 14, 2018 at 14:55

3 Answers 3

13

You can use a combination of strtotime() and checkdate() to see if the date is valid:

function isRealDate($date) { 
    if (false === strtotime($date)) { 
        return false;
    } 
    list($year, $month, $day) = explode('-', $date); 
    return checkdate($month, $day, $year);
}

usage

if (isRealDate($geboortedatum)) {
    // date is ok
}
else {
    // date is not ok
}
Sign up to request clarification or add additional context in comments.

2 Comments

Can't get this code to work. How can I call this function? Something like this: isRealDate($geboortedatum); It never returns anything :(
This works pretty good... Although a year like 1900 doesn't seem to be working. But a year like 2 does, guess because of strtotime limitations. So I'll make this the accepted answer. Thanks!
4

With DateTime you can make the shortest date&time validator for all formats.

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

var_dump(validateDate('2012-02-28 12:12:12')); # true
var_dump(validateDate('2012-02-30 12:12:12')); # false
var_dump(validateDate('2012-02-28', 'Y-m-d')); # true
var_dump(validateDate('28/02/2012', 'd/m/Y')); # true
var_dump(validateDate('30/02/2012', 'd/m/Y')); # false
var_dump(validateDate('14:50', 'H:i')); # true
var_dump(validateDate('14:77', 'H:i')); # false
var_dump(validateDate(14, 'H')); # true
var_dump(validateDate('14', 'H')); # true

var_dump(validateDate('2012-02-28T12:12:12+02:00', 'Y-m-d\TH:i:sP')); # true
# or
var_dump(validateDate('2012-02-28T12:12:12+02:00', DateTime::ATOM)); # true

var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', 'D, d M Y H:i:s O')); # true
# or
var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', DateTime::RSS)); # true
var_dump(validateDate('Tue, 27 Feb 2012 12:12:12 +0200', DateTime::RSS)); # falsede here

function was copied from this answer or php.net

Comments

1
function isValidDateTime($dateTime)
{    
    if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-])$/", $dateTime, $matches)) 
    {     
        if (checkdate($matches[2],$matches[3], $matches[1])) {             
            return true;        
        }    
    }     
    return false; 
} 

Try the above code.

6 Comments

Not working... Try for yourself inputting 31/31/2010. It will still say valid date, although it isn't :(
I want to try this, but how can I call the function? Something like: isValidDateTime($geboortedatum);???
Not working... function isValidDateTime($dateTime) { echo "function called"; if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $dateTime, $matches)) { if (checkdate($matches[2],$matches[3], $matches[1])) { return true; echo "x"; } else { echo "y"; } } return false; echo "z"; } isValidDateTime("1990-01-01"); It only returns "function called", nothing else :(
What parameters are you passing? Hav you ever worked with functions before?
Yes, I got to have a function working before. This is what I passed: isValidDateTime("1990-01-01");
|

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.