I want to see if a time I read from a db overlaps with a time provided by a user.
My database looks like this:
-----------------------------------------------
|organiser|meeting_start|meeting_end|boardroom|
-----------------------------------------------
| John Doe| 1340193600 | 1340195400| big |
-----------------------------------------------
My code looks like this:
date_default_timezone_set('Africa/Johannesburg');
$from = strtotime($_GET['meeting_date'] . ' ' . $_GET['meeting_start']);
$to = strtotime($_GET['meeting_date'] . ' ' . $_GET['meeting_end']);
$another_meeting = false;
$meeting_date = strtotime($_GET['meeting_date']);
$meeting_next = $meeting_date + 86400;
$result = mysql_query("SELECT meeting_start, meeting_end FROM admin_boardroom_booking WHERE boardroom = '" . $_GET['boardroom'] . "' AND meeting_start >= '" . $meeting_date . "' AND meeting_end < '" . $meeting_next . "'")or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$from_compare = $row['meeting_start'];
$to_compare = $row['meeting_end'];
$intersect = min($to, $to_compare) - max($from, $from_compare);
if ( $intersect < 0 )
$intersect = 0;
$overlap = $intersect / 3600;
if ( $overlap <= 0 ) {
$another_meeting = true;
break;
}
}
if ($another_meeting)
echo 'ERROR';
If I type two overlapping times on purpose, it doesn't echo error. What am I doing wrong?
mysql_
functions are soon-to-be-deprecated, you should use MySQLi instead. – Kerenkeresan