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

前端 未结 16 2491
名媛妹妹
名媛妹妹 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 18:00

    Use in simple way with php prebuilt function:

    function checkmydate($date) {
      $tempDate = explode('-', $date);
      // checkdate(month, day, year)
      return checkdate($tempDate[1], $tempDate[2], $tempDate[0]);
    }
    

    Test

       checkmydate('2015-12-01'); //true
       checkmydate('2015-14-04'); //false
    

提交回复
热议问题