PHP - 检查日期范围

PHP - 检查日期范围

问题描述:

我有一个数据库表period。该表中的字段是:code | name | status | open_date | close_date。日期格式为Y/m/dPHP - 检查日期范围

这就是我想要的:如果今天的日期超出了数据库中的日期范围,那么该网站将不会显示表格和表格列。

这是我的代码:

<?php 
$sql5 = "select close_date from period" 
     . " where status='1'"; 
$result5 = mysqli_query($link, $sql5); 
if (!$result5) { 
    die("<h3>SQL Error</h3>" . $sql5); 
} 
$row5 = mysqli_fetch_array($result5); 

$date = date("Y/m/d"); 
?> 

<?php 
if($date >= $row3['open_date'] && $date <= $row5['close_date']) { 
?> 
<form action="manageInputPerwalian.php" method="POST"> 
    <table align="center"> 
     <tr> 
       <td>MK Code:</td> 
        <td><input type="text" name="mkCode"/></td> 
     </tr>  
     <tr> 
       <td>KP:</td> 
       <td><input type="text" name="kp"/></td> 
     </tr> 
      <tr> 
        <td></td> 
        <td><input type="submit" value="SUBMIT"/></td> 
      </tr> 
</table> 
</form> 
<?php } ?> 


<table border="1" style="width:100%"> 
<tr> 
    <th>MK CODE</th> 
      <th>MK NAME</th> 
      <th>CLASS</th> 
      <?php if($date >= $row3['open_date'] && $date <= $row5['close_date']) { ?> 
       <th>CANCEL</th> 
      <?php } ?> 
    </tr> 
</table> 

但是,无论是今天的日期是超出范围或范围,形式和表列将不会出现。怎么了?请解释你的答案。由于

+0

试试这个:'如果(的strtotime($日期)> =的strtotime($ ROW3 [ 'open_date'])&&的strtotime($日期)

+1

@FrayneKonok它的工作。谢谢 –

使用strtotime()转换的日期时间戳和比较,为你做得对

$openDateStr = strtotime($row3['open_date']); 
$closeDateStr = strtotime($row5['close_date']); 
$todayStr = strtotime(date("Y/m/d")); 
//compare 
if($todayStr >= $openDateStr && $todayStr <= $closeDateStr) { 
.. 
+1

@FrayneKonok真的,可能是我输入答案时有点迟了.. :)你可以添加你的答案,如果你在我回答之前在评论中添加它..在这种情况下,我会删除我的.. :) –

+0

它的工作。我不知道我可以使用操作符串。谢谢 –

一切。只有一些小错误。

  1. 没有$ row3变量。仅限$ row5。所以将row3更改为row5。
  2. 在选择查询中,您只选择了close_date。您还需要选择open_date。
  3. 接下来使用strtotime()函数进行比较。
+0

糟糕,我忘记了在这里键入open_date查询,但我已经在我的实际代码中存储了它,它存储在var $ row3中。谢谢。也许你想通过在答案中添加数字1和2来编辑我的问题? –

+0

是的。我编辑过。 @GraceMichelle –

你的代码没有任何真正的工作。只有几个矿工修复。 $row3应该在您的代码中可能是$row5

// Create a DateTime object 
$now = new DateTime(); 

$stmt = "select close_date 
from period 
where status='1' 
"; 

$result5 = mysqli_query($link, $sql5); 

if (!$result5) { 
    die("<h3>SQL Error</h3>" . $sql5); 
} 
$row5 = mysqli_fetch_array($result5); 

$date = date("Y/m/d"); 

然后,你的HTML

<!-- Note that you were using $row3 here --> 
<?php if ($now->format('Y-m-d') >= $row5['open_date'] && $now->format('Y-m-d') <= $row5['close_date']): ?> 
<form action="manageInputPerwalian.php" method="POST"> 
    <table align="center"> 
     <tr> 
       <td>MK Code:</td> 
        <td><input type="text" name="mkCode"/></td> 
     </tr> 
     <tr> 
      <td>KP:</td> 
      <td><input type="text" name="kp"/></td> 
     </tr> 
     <tr> 
      <td></td> 
      <td><input type="submit" value="SUBMIT"/></td> 
     </tr> 
</table> 
</form> 
<?php èndif; ?> 


<table border="1" style="width:100%"> 
<tr> 
    <th>MK CODE</th> 
      <th>MK NAME</th> 
      <th>CLASS</th> 
      <?php if ($now->format('Y-m-d') >= $row5['open_date'] && $now->format('Y-m-d') <= $row5['close_date']): ?> 
       <th>CANCEL</th> 
      <?php endif; ?> 
    </tr> 
</table>