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

前端 未结 23 1578
终归单人心
终归单人心 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条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 10:47

    You can use a preg_match with a checkdate php function

    $date  = "2012-10-05";
    $split = array();
    if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $split))
    {
        return checkdate($split[2], $split[3], $split[1]);
    }
    
    return false;
    

提交回复
热议问题