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

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

        /**** date check is a recursive function. it's need 3 argument 
        MONTH,DAY,YEAR. ******/
    
        $always_valid_date = $this->date_check($month,$day,$year);
    
        private function date_check($month,$day,$year){
    
            /** checkdate() is a php function that check a date is valid 
            or not. if valid date it's return true else false.   **/
    
            $status = checkdate($month,$day,$year);
    
            if($status == true){
    
                $always_valid_date = $year . '-' . $month . '-' . $day;
    
                return $always_valid_date;
    
            }else{
                $day = ($day - 1);
    
                /**recursive call**/
    
                return $this->date_check($month,$day,$year);
            }
    
        }
    

提交回复
热议问题