Skip to content
PHP Logo

How can overlapping time windows in a PHP table be prevented?

To prevent overlapping time windows in a PHP table, you can implement a check when inserting or updating records to ensure that the new time window does not overlap with any existing ones. This can be done by querying the database to check if there are any records that have a start time before the new end time and an end time after the new start time. If any such records are found, the insertion or update can be rejected.

php
// Assuming $newStartTime and $newEndTime are the start and end times of the new record

// Check for overlapping time windows
$query = "SELECT * FROM your_table WHERE (start_time < '$newEndTime' AND end_time > '$newStartTime')";
$result = mysqli_query($connection, $query);

if(mysqli_num_rows($result) > 0) {
    // Overlapping time windows found, reject insertion or update
    echo "Error: Overlapping time windows detected";
} else {
    // Proceed with insertion or update
    // Your code here
}