2015-07-02 158 views
1

我正在致力於會議室預訂系統,我花了他們想預訂房間的時間段。現在我有兩個時間段: 2016-06-14 13:00:07 from_time字段和2016-06-14 05:00:07作爲to_time字段在我的數據庫中。現在我必須檢查給定的時間段是否與任何其他給定時間段重疊或者不在數據庫中。怎麼做? 來存儲數據:我的代碼:如何檢查日期時間是否在兩個日期時間之間重疊?

$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('[email protected]','Video conferencing room registration',$message); 


    } 

回答

0

你會運行一個查詢像...

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

...如果你得到任何結果的話,另一個會議在那個時候和預訂你不要插入預訂。

+0

當我試圖在我的查詢中實現這個時,它總是顯示日期時間重疊! '$ reason = mysql_real_escape_string($ _ POST ['reason']); $ sql1 =「SELECT * FROM'video_conf_room' WHERE('from''from_time'和'to_time')或('$ to'BETWEEN from_time' AND'to_time')」; $ response = mysql_query($ sql1,$ con)或die(mysql_error()); if(!$ response){ //執行註冊查詢 } else echo「Time overlapped」;' 它總是顯示「時間重疊」 –

+0

請告訴我應該做什麼修改? –

相關問題