How can the logic for handling time intervals in PHP be improved to prevent errors and ensure accurate results when checking for conflicts?

To prevent errors and ensure accurate results when checking for conflicts in time intervals in PHP, it is important to properly handle edge cases such as overlapping intervals or intervals that start or end at the same time. One way to improve the logic is to use the DateTime class to compare and manipulate time intervals effectively.

function checkForConflict($start1, $end1, $start2, $end2) {
    $interval1 = new DateInterval("PT0S");
    $interval1->s = $start1;
    $interval1->e = $end1;

    $interval2 = new DateInterval("PT0S");
    $interval2->s = $start2;
    $interval2->e = $end2;

    $conflict = false;

    if($interval1->s < $interval2->e && $interval1->e > $interval2->s) {
        $conflict = true;
    }

    return $conflict;
}

$start1 = 3600;
$end1 = 7200;
$start2 = 5400;
$end2 = 9000;

if(checkForConflict($start1, $end1, $start2, $end2)) {
    echo "There is a conflict between the two time intervals.";
} else {
    echo "There is no conflict between the two time intervals.";
}