Correctly determine if date string is a valid date in that format

前端 未结 16 2479
名媛妹妹
名媛妹妹 2020-11-22 17:02

I\'m receiving a date string from an API, and it is formatted as yyyy-mm-dd.

I am currently using a regex to validate the string format, which works ok,

16条回答
  •  长情又很酷
    2020-11-22 17:44

    /*********************************************************************************
    Returns TRUE if the input parameter is a valid date string in "YYYY-MM-DD" format (aka "MySQL date format")
    The date separator can be only the '-' character.
    *********************************************************************************/
    function isMysqlDate($yyyymmdd)
    {
        return checkdate(substr($yyyymmdd, 5, 2), substr($yyyymmdd, 8), substr($yyyymmdd, 0, 4)) 
            && (substr($yyyymmdd, 4, 1) === '-') 
            && (substr($yyyymmdd, 7, 1) === '-');
    }
    

提交回复
热议问题