[英]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.
仅一些小错误。
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.