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

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

    Validate with checkdate function:

    $date = '2019-02-30';
    
    $date_parts = explode( '-', $date );
    
    if(checkdate( $date_parts[1], $date_parts[2], $date_parts[0] )){
        //date is valid
    }else{
        //date is invalid
    }
    

提交回复
热议问题