带错误检查的日期检查程序

时间:2013-11-26 16:51:38

标签: php date

这是我要找的东西。

  1. 我需要start_dateend_date的日期选择器。
  2. 当用户输入错误时,应该会出错。 (例如:start_date:27/11/2013和end_date:29/11/2013)并返回同一页面。
  3. 我的试验是这样的。

    1. 为日,月和年创建jumpMenus
    2. 使用strtotime()查找差异。
    3. 如果小于0存储生成错误标志并返回当前页面。
    4. 但那没用。 (我没有包含代码,因为我想要更好的东西,我知道这不是正确的方法。)

      有人可以指导我如何制作作品吗?

      这是我多年来尝试过的: Html表格:

             <label>Trip Start date</label>
                   <select name="day" id="tripStart" onchange="MM_jumpMenu('parent',this,0)">
                        <option>01</option>
                        .
                        <option>31</option>
                   </select>
                   <select name="month">
                        <option>Jan</option>
                         .
                         .
                         <option>Dec</option>
                    </select>
                    <select name="year">
                         <option>2013</option>
                         .
                         .
                         <option>2024</option>
                    </select>
            </p>
          </td>
          <td width="315"><label>Trip End date</label>
                    <select name="day2" onchange="MM_jumpMenu('parent',this,0)">
                          <option>01</option>
                          .
                          .
                          <option>31</option>
                    </select>
                    <select name="month2">
                           <option>Jan</option>
                           .
                           .
                           <option>Dec</option>
                    </select>
                    <select name="year2">
                            <option>2013</option>
                          .
                          .
                          <option>2024</option>
                    </select>
         </td>
         </tr>
      
          <?php 
             if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) &&    count($_SESSION['ERRMSG_ARR']) >0 )  {
              echo '<ul class="err">';
              foreach($_SESSION['ERRMSG_ARR'] as $msg) {
                  echo '<li>',$msg,'</li>'; 
                  }
              echo '</ul>';
              unset($_SESSION['ERRMSG_ARR']);
              }
           ?>  
      

      在verify.php中

         $y_start = $_POST['year'];
         $y_end = $_POST['year2'];
      
           $diff = (strtotime($y_start) - strtotime($y_end ));
           $years = floor($diff / (365*60*60*24));
      
               if ($year < 0) {
              $errmsg_arr[] = 'Please enter a valid year!';
              $errflag = true;
                }
      
               if($errflag){
              $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
              session_write_close();
              header("location: /application/home.php");
              exit();
          }
      

2 个答案:

答案 0 :(得分:0)

试试这个:

<?php

if(isset($_POST['action']) && $_POST['action']=='check' 
    && isset($_POST['fday']) && isset($_POST['tday']) 
    && isset($_POST['fmonth']) && isset($_POST['tmonth']) 
    && isset($_POST['fyear']) && isset($_POST['tyear']) 
    && ctype_digit($_POST['fday']) && ctype_digit($_POST['tday']) 
    && ctype_digit($_POST['fmonth']) && ctype_digit($_POST['tmonth']) 
    && ctype_digit($_POST['fyear']) && ctype_digit($_POST['tyear'])
){

    $error = 0;

    $fday = sprintf("%02s", $_POST['fday']);
    $fmonth = sprintf("%02s", $_POST['fmonth']);
    $fyear = $_POST['fyear'];
    $fdate = $fday.'/'.$fmonth.'/'.$fyear;

    $tday = sprintf("%02s", $_POST['tday']);
    $tmonth = sprintf("%02s", $_POST['tmonth']);
    $tyear = $_POST['tyear'];
    $tdate = $tday.'/'.$tmonth.'/'.$tyear;

    if(!checkdate($_POST['fmonth'], $_POST['fday'], $_POST['fyear'])){
        $err_startdate = 1;
        $error = 1;
    }
    if(!checkdate($_POST['tmonth'], $_POST['tday'], $_POST['tyear'])){
        $err_enddate = 1;
        $error = 1;
    }



    $format = "d/m/Y";
    $from_date  = DateTime::createFromFormat($format, $fdate);
    $to_date  = DateTime::createFromFormat($format, $tdate);

    if($from_date > $to_date){
        $err_compare = 1;
        $error = 1;
    }

    if($from_date == $to_date){
        $err_equal = 1;
        $error = 1;
    }

    if(isset($error) && $error == 1){
        if(isset($err_startdate))
        echo '<h1 style="color:lightcoral;">Your starting date is invalid</h1>';
        if(isset($err_enddate))
        echo '<h1 style="color:lightcoral;">Your ending date is invalid</h1>';
        if(isset($err_compare))
        echo '<h1 style="color:lightcoral;">Your starting date is after your ending date</h1>';
        if(isset($err_equal))
        echo '<h1 style="color:lightcoral;">Your starting date is equal to the ending date</h1>';

    }else {

        echo '<h1 style="color:lightgreen;">Success!</h1>';
        echo 'Execute query here with from date: '.$fdate.' and to date: '.$tdate.'<br><br>';
    }
}

if(!isset($error) || $error == 1){

?>


<form method="post">

    <input type="hidden" name="action" value="check">
    <table>
    <tr>
        <td colspan="3">
        From Date:
        </td>
        <td>&nbsp;</td>
        <td colspan="3">
        To Date:
        </td>
    </tr>
    <tr>
        <td>
        <label for="fmonth" style='font-size: 1.2em;'>Month</label><br>
        <select name="fmonth" id="month">
            <?php
            for($index = 1; $index <= 12; $index++){
            echo '<option';

            if(isset($_POST['fmonth'])){
                if($index == $_POST['fmonth'])
                echo ' selected >';
                else
                echo '>';
            }else{
                if($index == date('m'))
                echo ' selected >';
                else
                echo '>';
            }

            echo $index.'</option>';


            }
            ?>
        </select>
        </td>
        <td>
        <label for="fday" style='font-size: 1.2em;'>Day</label><br>
        <select name="fday" id="day">
            <?php
            for($index = 1; $index <= 31; $index++){
            echo '<option';

            if(isset($_POST['fday'])){
                if($index == $_POST['fday'])
                echo ' selected >';
                else
                echo '>';
            }else{
                if($index == date('d'))
                echo ' selected >';
                else
                echo '>';
            }

            echo $index.'</option>';

            }
            ?>
        </select>
        </td>
        <td>
        <label for="fyear" style='font-size: 1.2em;'>Year</label><br/>
        <select name="fyear" id="year">
            <?php
            for($index = 2013; $index <= 2016; $index++){
            echo '<option';

            if(isset($_POST['fyear'])){
                if($index == $_POST['fyear'])
                echo ' selected >';
                else
                echo '>';
            }else{
                if($index == date('Y'))
                echo ' selected >';
                else
                echo '>';
            }

            echo $index.'</option>';

            }
            ?>
        </select>
        </td>
        <td> - </td>
        <td>
        <label for="tmonth" style='font-size: 1.2em;'>Month</label><br/>
        <select name="tmonth" id="tmonth">
            <?php
            for($index = 1; $index <= 12; $index++){
            echo '<option';

            if(isset($_POST['tmonth'])){
                if($index == $_POST['tmonth'])
                echo ' selected >';
                else
                echo '>';
            }else{
                if($index == date('m'))
                echo ' selected >';
                else
                echo '>';
            }

            echo $index.'</option>';


            }
            ?>
        </select>
        </td>
        <td>
        <label for="tday" style='font-size: 1.2em;'>Day</label><br/>
        <select name="tday" id="tday">
            <?php
            for($index = 1; $index <= 31; $index++){
            echo '<option';

            if(isset($_POST['tday'])){
                if($index == $_POST['tday'])
                echo ' selected >';
                else
                echo '>';
            }else{
                if($index == date('d'))
                echo ' selected >';
                else
                echo '>';
            }

            echo $index.'</option>';

            }
            ?>
        </select>
        </td>
        <td>
        <label for="tyear" style='font-size: 1.2em;'>Year</label><br/>
        <select name="tyear" id="tyear">
            <?php
            for($index = 2013; $index <= 2016; $index++){
            echo '<option';

            if(isset($_POST['tyear'])){
                if($index == $_POST['tyear'])
                echo ' selected >';
                else
                echo '>';
            }else{
                if($index == date('Y'))
                echo ' selected >';
                else
                echo '>';
            }

            echo $index.'</option>';

            }
            ?>
        </select>
        </td>
    </tr>
    </table>
       <input type="submit" value="Submit" />
</form>

<?php } ?>

答案 1 :(得分:0)

从PHP 5.2.2开始,可以使用comparison operators比较DateTime对象。

<?php
$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");

var_dump($date1 == $date2);
var_dump($date1 < $date2);
var_dump($date1 > $date2);
?>

此方法适用于比较,但如果您对天数或小时的实际时差感兴趣,请使用Dennis C在PHP手册上的this代码。

<?php
function date_diff($date1, $date2) {
    $current = $date1;
    $datetime2 = date_create($date2);
    $count = 0;
    while(date_create($current) < $datetime2){
        $current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current)));
        $count++;
    }
    return $count;
}

echo (date_diff('2010-3-9', '2011-4-10')." days <br \>");
?>