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!

Nick
  • 169
  • 1
  • 6
  • 16
  • 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. – Marc B Jul 10 '13 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... – Nick Jul 10 '13 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. – Marc B Jul 10 '13 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... – Nick Jul 10 '13 at 18:20
  • similar https://stackoverflow.com/questions/19271381/correctly-determine-if-date-string-is-a-valid-date-in-that-format/31133321 or from manual http://php.net/manual/en/function.checkdate.php#113205 – Vladimir Ch May 14 '18 at 14:55

3 Answers3

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
}
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Can't get this code to work. How can I call this function? Something like this: isRealDate($geboortedatum); It never returns anything :( – Nick Jul 10 '13 at 18:21
  • 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! – Nick Jul 10 '13 at 18:44
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

Community
  • 1
  • 1
Sargis Isoyan
  • 1,049
  • 8
  • 15
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.

Benz
  • 2,317
  • 12
  • 19
  • Not working... Try for yourself inputting 31/31/2010. It will still say valid date, although it isn't :( – Nick Jul 10 '13 at 18:11
  • I want to try this, but how can I call the function? Something like: isValidDateTime($geboortedatum);??? – Nick Jul 10 '13 at 18:24
  • 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 :( – Nick Jul 10 '13 at 18:32
  • What parameters are you passing? Hav you ever worked with functions before? –  Jul 10 '13 at 18:33
  • Yes, I got to have a function working before. This is what I passed: isValidDateTime("1990-01-01"); – Nick Jul 10 '13 at 18:34
  • My function intakes date and time y-m-d h:i:s –  Jul 10 '13 at 18:37