PHP Regex to check date is in YYYY-MM-DD format

前端 未结 23 1411
终归单人心
终归单人心 2020-11-27 10:05

I\'m trying to check that dates entered by end users are in the YYYY-MM-DD. Regex has never been my strong point, I keep getting a false return value for the preg_match() I

相关标签:
23条回答
  • To work with dates in php you should follow the php standard so the given regex will assure that you have valid date that can operate with PHP.

        preg_match("/^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date)
    
    0 讨论(0)
  • 2020-11-27 10:22

    You can make it this way:

    if (preg_match("/\d{4}\-\d{2}-\d{2}/", $date)) {
        echo 'true';
    } else {
        echo 'false';
    }
    

    but you'd better use this one:

    $date = DateTime::createFromFormat('Y-m-d', $date);
    if ($date) {
        echo $date -> format('Y-m-d');
    }
    

    in this case you'll get an object which is muck easier to use than just strings.

    0 讨论(0)
  • 2020-11-27 10:22

    you can use

    function validateDate($date, $format = 'Y-m-d H:i:s')
    {
        $d = DateTime::createFromFormat($format, $date);
        return $d && $d->format($format) == $date;
    }
    
    $date="2012-09-12";    
    echo validateDate($date, 'Y-m-d'); // true or false
    
    0 讨论(0)
  • 2020-11-27 10:22

    Check and validate YYYY-MM-DD date in one line statement

    function isValidDate($date) {
        return preg_match("/^(\d{4})-(\d{1,2})-(\d{1,2})$/", $date, $m)
            ? checkdate(intval($m[2]), intval($m[3]), intval($m[1]))
            : false;
    }
    

    The output will be:

    var_dump(isValidDate("2018-01-01")); // bool(true)
    var_dump(isValidDate("2018-1-1"));   // bool(true)
    var_dump(isValidDate("2018-02-28")); // bool(true)
    var_dump(isValidDate("2018-02-30")); // bool(false)
    

    Day and month without leading zero are allowed. If you don't want to allow this, the regexp should be:

    "/^(\d{4})-(\d{2})-(\d{2})$/"
    
    0 讨论(0)
  • 2020-11-27 10:22

    I was searching "how to validate date" and found this solution, Its old but i ll share below method that can be use to validate date in php,

    checkdate('01', '31', '2019')
    
    0 讨论(0)
  • 2020-11-27 10:25

    Probably useful to someone:

    $patterns = array(
                'Y'           =>'/^[0-9]{4}$/',
                'Y-m'         =>'/^[0-9]{4}(-|\/)([1-9]|0[1-9]|1[0-2])$/',
                'Y-m-d'       =>'/^[0-9]{4}(-|\/)([1-9]|0[1-9]|1[0-2])(-|\/)([1-9]|0[1-9]|[1-2][0-9]|3[0-1])$/',
                'Y-m-d H'     =>'/^[0-9]{4}(-|\/)([1-9]|0[1-9]|1[0-2])(-|\/)([1-9]|0[1-9]|[1-2][0-9]|3[0-1])\s(0|[0-1][0-9]|2[0-4])$/',
                'Y-m-d H:i'   =>'/^[0-9]{4}(-|\/)([1-9]|0[1-9]|1[0-2])(-|\/)([1-9]|0[1-9]|[1-2][0-9]|3[0-1])\s(0|[0-1][0-9]|2[0-4]):?(0|[0-5][0-9]|60)$/',
                'Y-m-d H:i:s' =>'/^[0-9]{4}(-|\/)([1-9]|0[1-9]|1[0-2])(-|\/)([1-9]|0[1-9]|[1-2][0-9]|3[0-1])\s(0|[0-1][0-9]|2[0-4]):?((0|[0-5][0-9]):?(0|[0-5][0-9])|6000|60:00)$/',
            );
    echo preg_match($patterns['Y'], '1996'); // true
    echo preg_match($patterns['Y'], '19966'); // false
    echo preg_match($patterns['Y'], '199z'); // false
    echo preg_match($patterns['Y-m'], '1996-0'); // false
    echo preg_match($patterns['Y-m'], '1996-09'); // true
    echo preg_match($patterns['Y-m'], '1996-1'); // true
    echo preg_match($patterns['Y-m'], '1996/1'); // true
    echo preg_match($patterns['Y-m'], '1996/12'); // true
    echo preg_match($patterns['Y-m'], '1996/13'); // false
    echo preg_match($patterns['Y-m-d'], '1996-11-1'); // true
    echo preg_match($patterns['Y-m-d'], '1996-11-0'); // false
    echo preg_match($patterns['Y-m-d'], '1996-11-32'); // false
    echo preg_match($patterns['Y-m-d H'], '1996-11-31 0'); // true
    echo preg_match($patterns['Y-m-d H'], '1996-11-31 00'); // true
    echo preg_match($patterns['Y-m-d H'], '1996-11-31 24'); // true
    echo preg_match($patterns['Y-m-d H'], '1996-11-31 25'); // false
    echo preg_match($patterns['Y-m-d H:i'], '1996-11-31 2400'); // true
    echo preg_match($patterns['Y-m-d H:i'], '1996-11-31 24:00'); // true
    echo preg_match($patterns['Y-m-d H:i'], '1996-11-31 24:59'); // true
    echo preg_match($patterns['Y-m-d H:i'], '1996-11-31 24:60'); // true
    echo preg_match($patterns['Y-m-d H:i'], '1996-11-31 24:61'); // false
    echo preg_match($patterns['Y-m-d H:i'], '1996-11-31 24:61'); // false
    echo preg_match($patterns['Y-m-d H:i:s'], '1996-11-31 24:6000'); // true
    echo preg_match($patterns['Y-m-d H:i:s'], '1996-11-31 24:60:00'); // true
    echo preg_match($patterns['Y-m-d H:i:s'], '1996-11-31 24:59:59'); // true
    echo preg_match($patterns['Y-m-d H:i:s'], '1996-11-31 24:59:60'); // false
    echo preg_match($patterns['Y-m-d H:i:s'], '1996-11-31 24:60:01'); // false
    
    0 讨论(0)
提交回复
热议问题