php - 选择 MySQL 范围内的所有时间戳

标签 php mysql unix-timestamp date-range

我正在为我的项目开发类似日历的功能。

我有如下表格:

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"

我在选择一天内发生的所有事件时遇到问题。 我尝试了以下两个查询,但它们只返回那些在同一天开始和结束的事件。

/*
   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<?)

如果开始/结束范围“触及”查询的时间戳范围,MySQL 或 PHP 是否有某种方法可以匹配?

最佳答案

假设?的两个值是当天最早和最新的值,我想你想要:

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

也就是说,当一个开始在另一个结束之前时存在重叠,反之亦然。

在这个答案中,(?end) 是当天的最后一个时间戳,(?start) 是第一个。

关于php - 选择 MySQL 范围内的所有时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22640293/

相关文章:

php - JSON 文件存储在 PHP 变量中

mysql 随机和 desc 查询

mysql - Bash脚本创建数据库

mysql - 按升序获取最后 5 条记录,无需子查询

scala - Spark scala long 转换为 Parquet 数据帧中的毫秒时间戳

sql - 在 Hive 中创建具有当前时间戳(以纳秒为单位)的新列

javascript - 使用 ajax 发送 rowID 并将选定的行附加到新表

php - 如何使用下面的示例正确设置布局

unix - UNIX 目录何时更改其时间戳

php - 不同 PHP mcrypt 算法的速度