简体   繁体   English

如何检查日期时间是否在两个日期时间之间重叠?

[英]How to check whether the Date time is overlapping between two date times?

I am working on a conference room booking system and I take the time period for which they want to book the room. 我正在使用会议室预订系统,我花了他们想要预定房间的时间。 Now I have two time periods as : 2016-06-14 13:00:07 as from_time field and 2016-06-14 05:00:07 as to_time field in my database. 现在我在数据库中有两个时间段,分别是:from_time字段为2016-06-14 13:00:07和to_time字段为2016-06-14 05:00:07 Now I have to check whether the given period overlaps with any other given period or not in the database. 现在,我必须检查给定时间段是否与数据库中的任何其他给定时间段重叠。 How to do that? 怎么做? To store the data : My code : 存储数据:我的代码:

$date = mysql_real_escape_string($_POST['date']);
    $date1= mysql_real_escape_string($_POST['date1']);
    $from=mysql_real_escape_string($_POST['date2']);
    $to=mysql_real_escape_string($_POST['date3']);

    $from = strtotime($from);
    $to = strtotime($to);

    $subfrom =strtotime('-30 minutes',$from);
    $addto = strtotime('+30 minutes',$to);

    $from = date('Y-m-d H:i:s',$from);
    $to = date('Y-m-d H:i:s',$to);

    $subfrom = date('Y-m-d H:i:s',$subfrom);
    $addto =  date('Y-m-d H:i:s',$addto);

    $id = mysql_real_escape_string($_POST['faculty_id']);
    $name = mysql_real_escape_string($_POST['faculty_name']);
    $div = mysql_real_escape_string($_POST['division']);
    $school = mysql_real_escape_string($_POST['s_name']);
    $email = mysql_real_escape_string($_POST['email']);
    $contact = mysql_real_escape_string($_POST['contact_no']);
    $intercom = mysql_real_escape_string($_POST['intercom']);
    $purpose = mysql_real_escape_string($_POST['purpose']);
    if($purpose=='university lecture')
        $purpose = "university lecture";
    else if($purpose=='guest lecture')
        $purpose = "guest lecture";
    else if($purpose=='PHD Oral Exam')
        $purpose = "PHD Oral Exam";
    else if($purpose=='Placement Interview')
        $purpose = "Placement Interview";
    else if($purpose=='others')
        $purpose = "others";

    $reason = mysql_real_escape_string($_POST['reason']);

    $sql = "insert into video_conf_room(employee_id, emp_name, division, s_name, email, contact_no, intercom, from_time, to_time, b_f_time, b_t_time, purpose, reason) 
            values('$id','$name','$div','$school','$email','$contact','$intercom','$from','$to','$subfrom','$addto','$purpose','$reason')";

     $res = mysql_query($sql, $con) or die(mysql_error());
     if($res)
     {
        echo "you have successfully booked the video comferencing room";
        $message = "Following are the details for the video conferencing room registration booked recently \r\n Faculty id: ".$id."\r\nName: ".$name."\r\n Divison: "
                    .$div."\r\n School: ".$school."\r\n email: ".$email."\r\n Contact: ".$contact."\r\n Intercom no.: ".$intercom."\r\n Purpose: ".
                    $purpose."\r\n Reason: ".$reason."\r\n Booked from :".$subfrom."Booked till :".$addto;
        $message = wordwrap($message, 70, "\r\n");

        mail('mymail@domain.com','Video conferencing room registration',$message);


     }

You would run a query like ... 您将运行类似...的查询

SELECT * FROM `video_conf_room` WHERE ('$from' BETWEEN `from_time` AND `to_time`) OR ('$to' BETWEEN `from_time` AND `to_time`)

... and if you get any results then another meeting is booked at that time and you don't insert the reservation. ...如果您得到任何结果,那么届时将预定另一次会议,并且您无需插入预订。

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

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