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

前端 未结 16 2437
名媛妹妹
名媛妹妹 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:56

    You can also Parse the date for month date and year and then you can use the PHP function checkdate() which you can read about here: http://php.net/manual/en/function.checkdate.php

    You can also try this one:

    $date="2013-13-01";
    
    if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date))
        {
            echo 'Date is valid';
        }else{
            echo 'Date is invalid';
        }
    

提交回复
热议问题