capstone
capstone

Reputation: 201

Check if user input is in between time

I have this system function where a user sends an invitation to a official. To illustrate, the official has in his calendar a 3-day retreat. In the database, it looks like this:

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

The user wants to send a two-day event invitation to the official on 2018-01-17 to 2018-01-19 at 10:00AM to 11:00AM for those days. How do I reject his input if his proposed date and time are anywhere in between the current date and time in the database?

So far, this is what I've worked with

$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>";
}

Any help would be appreciated

Upvotes: 0

Views: 52

Answers (1)

Andrew
Andrew

Reputation: 1596

MySQL has a BETWEEN condition:

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

Assuming the query worked before, this will find you what you were looking for.

Upvotes: 1

Related Questions