带有错误检查的日期检查器


Date checker with error check

这是我在寻找的一些东西。

  1. 我需要一个用于start_dateend_date的日期选择器。
  2. 当用户输入错误时,它应该给出一些错误。(例如:start_date:27/11/2013和end_date:29/11/2013(并返回同一页面。

我的试炼是这样的。

  1. 创建了日、月和年的jumpMenus
  2. strtotime()来查找差异。
  3. 如果小于0存储生成错误标志并返回到当前页面。

但这没有用。(我没有包含代码,因为我想要更好的东西,我知道这不是正确的方法。

谁能指导我如何创建解决方法?

这是我多年来尝试过的:网页形式:

       <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']);
        }
     ?>  

在验证中.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();
    }

试试这个:

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

从 PHP 5.2.2 开始,可以使用比较运算符来比较日期时间对象。

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

此方法非常适合比较,但如果您对天或小时的实际时差感兴趣,请使用 php 手册上的 Dennis C 的这段代码。

<?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 '>");
?>