2016-11-04 29 views
0

我想了解如何管理mysql上的日期,並且我已經給予練習來練習它。php checkdate,單獨檢查每個參數

在輸入上,用戶必須添加格式爲dd-mm-YYYY的日期,然後如果日期有效,我將記錄在數據庫上,將格式更改爲YYYY-mm-dd,因爲它是

該如何日期變量類型存儲(我的理解),但是,我還沒有到來......是我到目前爲止有:

HTML

<form method="POST" action="testing.php"> 
     <div> 
      <label for="title" class="required">Title</label> 
      <input name="title" id="title" type="text" value="" /> 
     </div> 
     <div> 
      <label for="date" class="required">Date</label> 
      <input name="date" id="date" type="date" value="" /> 
     </div> 
     <button class="submit" type="submit" name="submit">Submit</button> 
     </form> 

PHP

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    $title = filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING); 
    $string_input_date = filter_input(INPUT_POST, 'date', FILTER_SANITIZE_STRING); 
    $array_input_date = explode("-", $string_input_date); 
    if(!checkdate($array_input_date[1], $array_input_date[0], $array_input_date[2])) { 

     if(checkdate(!$array_input_date[1], $array_input_date[0], $array_input_date[2])) { 
      $error_message = "wrong month"; die($error_message); 
     } elseif (checkdate($array_input_date[1], !$array_input_date[0], $array_input_date[2])) { 
      $error_message = "wrong day"; 
     } elseif (checkdate($array_input_date[1], $array_input_date[0], !$array_input_date[2])) { 
      $error_message = "wrong year"; 
     } else { 
       $error_message = "Introduce a valid date"; 
     } 
    } else { 
     //success! it is a valid date 
    } 
} 

我可以看到,它不工作,但我用完了關於如何檢查每個參數的選項......目標是,如果是一天,我必須顯示錯誤消息錯誤的,或者一個月,或者一年。

目前,我嘗試介紹35-04-2016應顯示錯誤消息「錯誤的一天」,而是顯示「引入有效日期」。我知道它顯示,因爲我的if-elseif是錯誤的,但是...有關如何分別檢查每個參數的任何建議?

PS:我知道我可以用正則表達式來做,或者只是創建自己的規則,像一個月是在1-12等等等等,但我的導師說我必須使用PHP功能和內置函數....所以我解決不了的創建我自己的規則或使用正則表達式(現在,我想如果他看到我哭,他會告訴我,做我想做的事情^^)

謝謝

回答

0

checkdate()僅基於整個日期返回true或false,它不會告訴你哪個部分是錯誤的。

如果你想知道哪個部分是錯誤的,你需要逐個檢查。

if(!checkdate($array_input_date[1], $array_input_date[0], 
    $array_input_date[2])) 
     { 
      if($array_input_date[1] < 0 || $array_input_date[1] > 12) 
      { 
       $error_message = "wrong month"; die($error_message); 
      } 
      elseif($array_input_date[2] < 0) 
      { 
       $error_message = "wrong year"; 
      } 
      else 
      { 
       $last_day = cal_days_in_month(CAL_GREGORIAN, $array_input_date[1], $array_input_date[2]); 
       if($array_input_date[0] < 0 || $array_input_date[0] > $last_day) 
       { 
        $error_message = "wrong day"; 
       } 
      } 
     } 
0

更新

您可以使用PHP DATE_PARSE的方法來實現。

if (false === strtotime($string_input_date)) { 
    echo 'Invalid Date'; 
} else { 
    $array_input_date = date_parse(date('Y-m-d H:i:s', strtotime($string_input_date))); 

    if (false !== checkdate($array_input_date['month'], $array_input_date['day'], $array_input_date['year'])) 
    { 
     echo 'Valid Date'; 
    } 
} 
+0

但是我處於同樣的情況,對嗎?如何比較給定日期是否有效?如果我解析數據...我試試35-04-2015: 它打印出來: [year] => 2035 [month] => 4 [day] => 20 所以它沒有檢測到這一天是錯誤的,但它也改變了給定的一年! –

+0

@eve_mf更新了我的答案。將** 35-04-2015 **傳遞給'strtotime'將返回false。所以,這將做第一次驗證,如果這還不夠,那麼你可以去'checkdate' – Haridarshan