简体   繁体   English

PHP-检查日期范围

[英]PHP - Check Date Range

I have a table on database called period . 我在数据库上有一张名为period的表。 The fields in this table are: code | name | status | open_date | close_date 此表中的字段有: code | name | status | open_date | close_date code | name | status | open_date | close_date code | name | status | open_date | close_date . code | name | status | open_date | close_date The date format is Y/m/d . 日期格式为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 谢谢

use strtotime() to convert the dates to timestamp and compare, as, 使用strtotime()将日期转换为时间戳并进行比较,

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

Everything you did right. 您所做的一切正确。 Some small mistakes only. 仅一些小错误。

  1. There is no $row3 variable. 没有$ row3变量。 Only $row5 only. 仅$ row5而已。 So change row3 to row5. 因此将row3更改为row5。
  2. In select query you selected only close_date. 在选择查询中,您仅选择了close_date。 you need to select open_date also. 您还需要选择open_date。
  3. Next use strtotime() functions to compare. 接下来使用strtotime()函数进行比较。

There's nothing really worng with your code. 您的代码并没有真正感到厌倦。 Just a few miner fixes. 只是一些矿工修复。 $row3 should probably be $row5 in your code. $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");

And then, your HTML 然后,您的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM