检查用户输入是否在时间之间

时间:2018-01-16 14:19:11

标签: php mysql

我有这个系统功能,用户向官员发送邀请。为了说明这位官员在他的日历中有一个为期3天的撤退。在数据库中,它看起来像这样:

+---------+------------+------------+--------+----------+
|  name   | date_start |  date_end  |  time  | time_end |
+---------+------------+------------+--------+----------+
| Retreat | 2018-01-16 | 2018-01-18 | 9:00AM | 12:00PM  |
| Retreat | 2018-01-17 | 2018-01-18 | 9:00AM | 12:00PM  |
| Retreat | 2018-01-18 | 2018-01-18 | 9:00AM | 12:00PM  |
+---------+------------+------------+--------+----------+

用户希望在2018-01-17 to 2018-01-19 10:00AM to 11:00AM处向这位官员发送为期两天的活动邀请。如果他的建议日期和时间在数据库中当前日期和时间之间的任何地方,我如何拒绝他的输入?

到目前为止,这是我使用过的

$q_date = mysqli_query($con, "SELECT * FROM `events` WHERE `date_start` = '$date' AND `time` = '$time_f' AND `pastor_id` = '$id'");
$count_date = mysqli_num_rows($q_date);
if ($count_date > 0) {
    echo "<p>Sorry, this time is occupied. Please select another schedule</p>";
}

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

MySQL有一个BETWEEN条件:

SELECT * FROM `events` 
WHERE 
('$date' BETWEEN `date_start` AND `date_end`)
AND
('$time_f' BETWEEN `time` AND `time_end`)
AND `pastor_id` = '$id'

假设查询之前有效,这将找到您要找的内容。