dongyi2425 2016-11-04 15:57
浏览 38
已采纳

php checkdate,单独检查每个参数

I am trying to learn about managing dates on mysql, and I have been given an exercise to practise it.

On an input, the user must add a date formated as dd-mm-YYYY, and then, if the date is valid, I record in on the database, changing the format to YYYY-mm-dd as it is how DATE variable types are stored (as I understood), but, I haven't arrived yet...

This is what I have so far:

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
    }
} 

I can see, it isn't working, but I run out of options about how can I check each argument one per one... The objective is, I have to display an error message if it is the day what is wrong, or the month, or the year.

At the moment, I try to introduce 35-04-2016 which should display the error message "wrong day", but instead it displays "Introduce a valid date". I know it is displaying that because my if-elseif are wrong but... any suggestion about how to check each argument separately?

P.S.: I know I could do it with regular expressions, or just creating myself rules like a month is between 1-12 etc etc, but my mentor says I have to use php features and built in functions.... so I CAN'T solve it creating my own rules or with regular expressions (for now, I guess if he sees me crying he might tell me to do whatever I want ^^)

Thank you

  • 写回答

2条回答 默认 最新

  • dsbm49845 2016-11-04 16:55
    关注

    checkdate() only returns true or false based on whole date, it won't tell you which part is wrong.

    If you want to know which part is wrong, you need to check one by one.

    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";
                    }
                }
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应