2013-11-26 56 views
-1

这里有一些事情正在寻找。错误检查日期检查器

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

我的试验是这样的。

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

但这并没有奏效。 (我没有包括代码,因为我想要更好的东西,我知道这不是正确的方式。)

任何人都可以指导我如何创建一个工作?

这是我曾尝试只年: 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(); 
    } 
+0

邮政编码,所以我们可以看到你在做什么...很难看到你究竟在问什么......你从来没有说过为什么它没有工作或任何关于你收到的错误。 –

+0

当我提交表单时,它会生成php。但没有显示错误。如果用户输入未来日期作为开始日期,我需要在主页的表单上生成错误。 – sajeesh

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

感谢玛娜和阿里安,但是我正在寻找类似于用户使旅程开始和旅程结束以存储在pdf和db中的形式的东西。 – sajeesh

+0

@ user2947210我更新了我的答案,它应该可以工作,您需要填写自己的数据库查询...但它应该可以正常工作,但我没有格式化该样式,但我确实将它放在了一个表格中。 –

+0

谢谢阿里安。它的作品 – sajeesh

0

随着PHP 5.2的。 2,DateTime对象可以使用comparison operators进行比较。

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

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

这是一个好方法作比较,但如果你是在天感兴趣的实际时间差或小时PH​​P手册使用this代码由Dennis℃。

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