1

I need to select all rows where start and due columns range falls into another range. All times are given in unix timestamp format.

The following query works, except when start < :weekstart or due > :weekend.

$week_start = (new DateTimeImmutable())->setTimestamp($_REQUEST['startdatets']);
$week_start_ts = $week_start->getTimestamp();
$week_end = $week_start->modify('+6 days')->setTime(23,59,59);
$week_end_ts = $week_end->getTimestamp();

$query = "  SELECT *

        FROM        schedules

        WHERE       ((start BETWEEN :weekstart AND :weekend) OR (due BETWEEN :weekstart AND :weekend))

        ORDER BY    start
";
try { $stmt = $dbh->prepare($query); $stmt->execute(array(':weekstart' => $week_start_ts, ':weekend' => $week_end_ts)); } catch(PDOException $ex) { echo 'Query failed: ' . $ex->getMessage(); exit; }

enter image description here

I need both to work if possible Please help.

Alex G
  • 3,048
  • 10
  • 39
  • 78

1 Answers1

1

Ahh, so you you're missing cases where both start < :weekstart AND end > :weekend - neither is in between, but the week overlaps the range. There's a nice logic for it.

Search for periods WHERE start < :weekend AND end > :weekstart

shudder
  • 2,076
  • 2
  • 20
  • 21