doushi1974 2016-05-30 04:28
浏览 30
已采纳

PHP - 检查日期范围

I have a table on database called period. The fields in this table are: code | name | status | open_date | close_date. The date format is Y/m/d.

This is what I want: if today's date is out of date range in the database, the site won't display a form and a table column.

This is my code:

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

But, either today's date is out of range or in the range, the form and the table column won't show up. What's wrong? Please explain your answer. Thanks

  • 写回答

3条回答 默认 最新

  • donglu7816 2016-05-30 04:34
    关注

    use strtotime() to convert the dates to timestamp and compare, as,

    $openDateStr = strtotime($row3['open_date']);
    $closeDateStr = strtotime($row5['close_date']);
    $todayStr = strtotime(date("Y/m/d"));
    //compare
    if($todayStr >= $openDateStr && $todayStr <= $closeDateStr) {
    ..
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥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代码移植没反应