这里有你需要完成这个任务的逻辑步骤:
步骤1独立的开始时间和结束时间为两列。所以,你的表应该是这样的:
time_id member_id entry_day time_in time_out
1 2 2016-01-19 2016-08-19 12:30:00 2016-08-18 03:00:00
你列的类型应该是:
`time_id`: `int`
`member_id`: `int`
`time_in`: `DATETIME`
`entry_day`: `DATE`
`time_out`: `DATETIME`
步骤2写一些PHP执行,看起来像这样在数据库上查询:
SELECT * FROM TimeEntryTable t
WHERE t.member_id = :memberId
AND :new_entry_time_in BETWEEN t.time_in AND t.time_out
OR :new_entry_time_out BETWEEN t.time_in AND t.time_out
您需要通过新记录time_in
,time_out
,member_id
。我假设你正在使用PDO ......如果没有,那么你应该开始。
步骤3
检查由该查询返回的结果。
if (count($resultsArray) === 0) {
//Create the new record,
} else {
//If you're here than there was a record within that time range and you should not create it.
}
为什么你还没有使用包括日期的'start time'和'end time'两列?使用查询来检查重复和交叉插槽会很容易。 –
确实如此,但无论如何,这并不难 – Strawberry
您可以在这3列上添加唯一约束条件 – Saurabh