我需要選擇start
和due
列範圍落入另一個範圍的所有行。所有時間都以unix時間戳格式給出。在另一個時間戳範圍內查找帶有時間戳範圍的所有行
以下查詢的工作原理除外start
< :weekstart
或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; }
我既需要如果可能的話 請幫助工作。
編輯您的問題並提供樣本數據和期望的結果。 –
[確定是否兩個日期範圍重疊]可能的重複(http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap) –
我投票結束這個問題,因爲你不清楚你在問什麼。 –