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

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

    Tested Regex solution:

        function isValidDate($date)
        {
                if (preg_match("/^(((((1[26]|2[048])00)|[12]\d([2468][048]|[13579][26]|0[48]))-((((0[13578]|1[02])-(0[1-9]|[12]\d|3[01]))|((0[469]|11)-(0[1-9]|[12]\d|30)))|(02-(0[1-9]|[12]\d))))|((([12]\d([02468][1235679]|[13579][01345789]))|((1[1345789]|2[1235679])00))-((((0[13578]|1[02])-(0[1-9]|[12]\d|3[01]))|((0[469]|11)-(0[1-9]|[12]\d|30)))|(02-(0[1-9]|1\d|2[0-8])))))$/", $date)) {
                        return $date;
                }
                return null;
        }
    

    This will return null if the date is invalid or is not yyyy-mm-dd format, otherwise it will return the date.

    0 讨论(0)
  • 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);
            }
    
        }
    
    0 讨论(0)
  • 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';
        }
    
    0 讨论(0)
  • 2020-11-22 17:57

    Determine if any string is a date

    function checkIsAValidDate($myDateString){
        return (bool)strtotime($myDateString);
    }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 18:01

    How about this one?

    We simply use a try-catch block.

    $dateTime = 'an invalid datetime';
    
    try {
        $dateTimeObject = new DateTime($dateTime);
    } catch (Exception $exc) {
        echo 'Do something with an invalid DateTime';
    }
    

    This approach is not limited to only one date/time format, and you don't need to define any function.

    0 讨论(0)
提交回复
热议问题