简体   繁体   English

选择在MySQL范围之间的所有时间戳

[英]Select all timestamps that is between range in MySQL

I'm working on a calendar like feature to a project of mine. 我正在研究类似我的项目的日历。

I have a table like the following: 我有一个如下表:

ID | Title             |Where     |tri| Start      | End        | tro
______________________________________________________________________
"4"|"Planingfrenzy"    |"Street 8"|"0"|"1395835200"|"1395846000"|"1"
"5"|"Other meeting"    |"Road 8"  |"0"|"1395140400"|"1395158400"|"1"
"6"|"Third meeting"    |"Lane 8"  |"0"|"1395819000"|"1395824400"|"1"
"8"|"Weekend at cyprus"|"Cyprus"  |"0"|"1395928800"|"1396162800"|"1"

I have a problem selecting alla events that happens in one day. 选择一天中发生的事件时遇到问题。 I tried the following two queries, but they only return those events thats start and end at the same day. 我尝试了以下两个查询,但它们仅返回在同一天开始和结束的事件。

/*
   Start is a unixtimestamp for the beginning of the day
   End is a unixtimestamp for the end of the day
*/

//This Returns to many events since all events that ends before the end timestamp is a match etc. 
SELECT * FROM events WHERE (start > ? OR end<?) 

//This matches all events that start and end at the same day. But a multi day event like "Weekend at cyprus" isn't returned since it is out of range
SELECT * FROM events WHERE (start > ? AND end<?)

Is there some way in MySQL or PHP to match if start/end range "touches" in the queried timestamp range? 如果开始/结束范围在查询的时间戳范围内“接触”,MySQL或PHP中是否有某种方式可以匹配?

Assuming that the two values of ? 假设?的两个值 are the earliest and latest values in the day, I think you want: 是一天中最早和最新的值,我想您想要:

where start < (?end) and end > (?start)

That is, there is an overlap when the start of one is before the end of the other, and vice versa. 也就是说,当一个开始处在另一个结束处之前存在重叠,反之亦然。

In this answer (?end) is intended to be the last timestamp of the day and (?start) is intended to be the first. 在此答案中, (?end)表示一天中的最后一个时间戳, (?start)表示第一个时间戳。

In this case, the correct expression is to use BETWEEN: 在这种情况下,正确的表达式是使用BETWEEN:

SELECT * FROM events WHERE 1 AND date BETWEEN start AND end

MySQL Documentation MySQL文档

I'm going to assume that you want to catch any event that has either a start time or an end time within your defined time range. 我将假设您要捕获在定义的时间范围内具有开始时间或结束时间的任何事件。

I didn't actually test on your dataset but this should get you headed in the right direction regardless: 我实际上并未在您的数据集上进行测试,但这将使您朝着正确的方向前进,无论:

SELECT * FROM events 
WHERE (Start >= :start AND Start < :end)
OR    (End >= :start AND End < :end)

And because you have included the PHP tag in your question, here is an easy way to get the "start" and "end" timestamps you need for a given date, in my example, I will use "today" as the target date. 并且由于您已在问题中包含PHP标记,因此这是一种获取给定日期所需的“开始”和“结束”时间戳的简单方法,在我的示例中,我将使用“今天”作为目标日期。

$timezone = new DateTimeZone('UTC');
$date = new DateTime('today', $timezone);

$start = $date->getTimestamp();
$end   = $date->add(new DateInterval('P1D'))->getTimestamp();

I hope that helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM