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

前端 未结 23 1566
终归单人心
终归单人心 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:30

    preg_match needs a / or another char as delimiter.

    preg_match("/^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$/",$date)
    

    you also should check for validity of that date so you wouldn't end up with something like 9999-19-38

    bool checkdate ( int $month , int $day , int $year )
    

提交回复
热议问题