douben7493 2014-10-30 12:40
浏览 36
已采纳

使用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. (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

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.

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

  • 写回答

1条回答 默认 最新

  • donglun1918 2014-10-30 12:52
    关注

    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.

    $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.

    If you decide to store lengths or end dates, we can help with the SQL if you have problems with it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程