简体   繁体   English

防止使用PHP / MySQL日期时间进行双重预订检查

[英]Prevent double booking check using PHP / MySQL datetime

Struggling with a lesson booking sys I am creating. 挣扎与我正在创建的课程预订系统。 I have a date in the db named lesson_date_time and it uses the mysql datetime and each lesson will last for a set number of minutes. 我在数据库中有一个名为lesson_date_time的日期,它使用mysql datetime,每节课将持续一定的分钟数。 (I am not storing the end date as minutes vary.) (由于分钟的不同,我不存储结束日期。)

To prevent any double bookings from the datetime stored plus required minutes, I can't seem to figure out the sql which will do a check to see if this time period exists 为了防止存储的日期时间加上所需的分钟数进行任何双重预订,我似乎无法弄清楚将检查该时间段是否存在的sql

I currently have... 我目前有...

$mins = "60";
$date_start = "2014-10-30 12:40:00";
$date_end   = date("Y-m-d H:i:s", strtotime("$date_start + $mins minute"));

WHERE lesson_date_time BETWEEN CAST('$date_start' AS DATETIME) AND CAST('$date_end' AS DATETIME)

This does the check and kinda works but does not catch any entries which have a start date in the hour (or time period until the end date) after the $start_date. 这样就可以执行检查并可以,但是不会捕获任何在$ start_date之后的小时(或直到结束日期的时间段)中具有开始日期的条目。

also tried 也尝试过

WHERE lesson_date_time >= '$date_start' and lesson_date_time =< '$date_end

which does not find any results even though they exit. 即使退出,也找不到任何结果。

Is it possible to do what I want to do or is it just easier if I store the lesson end time and go from there 是否可以做我想做的事情,还是如果我存储课程结束时间然后从那里走,那会更容易吗

Your between 你之间

WHERE lesson_date_time BETWEEN CAST('$date_start' AS DATETIME) AND CAST('$date_end' AS DATETIME)

would be equivilent to 等同于

WHERE lesson_date_time >= CAST('$date_start' AS DATETIME) AND lesson_date_time <= CAST('$date_end' AS DATETIME)

This still won't work since you are only looking for lessons that will start during this lesson. 由于您只是在寻找将在本课程中开始的课程,因此这仍然行不通。 You won't get ones that are continuing into this lesson. 您不会得到继续上这堂课的内容。 To get those, you would have to store an end date (easier) or store the lesson length. 要获得这些,您将必须存储结束日期(更容易)或存储课程长度。

If all lessons were 60 minutes long, you could do something like. 如果所有课程均为60分钟,您可以执行类似的操作。

$mins = "60";
$date_start = "2014-10-30 12:40:00";
$old_lesson_start = date("Y-m-d H:i:s", strtotime("$date_start - $mins minute"));
$date_end   = date("Y-m-d H:i:s", strtotime("$date_start + $mins minute"));

WHERE lesson_date_time BETWEEN CAST('$old_lesson_start' AS DATETIME) AND CAST('$date_end' AS DATETIME)

Then, it would catch lessons that started up to 60 minutes ago and so would be continuing into this lesson. 然后,它将捕获到60分钟之前开始的课程,因此将继续进行到本课程中。

If you decide to store lengths or end dates, we can help with the SQL if you have problems with it. 如果您决定存储长度或结束日期,如果您对SQL有疑问,我们可以为您提供帮助。

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

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