如何查找特定日期是否位于数据库的两个日期之间?

时间:2013-07-18 00:01:40

标签: php mysql date

<?php

 $hostname = "localhost";
 $db_user = "root";  
 $db_password = "";  
 $database = "rentcar";  
 $db_table = "reservation"; 

 $db = mysql_connect($hostname, $db_user, $db_password); 

 mysql_select_db($database,$db);

 $date1 = $_POST['datepicker1'];

 $date2 = $_POST['datepicker2'];


 $start_date = strtotime($date1);

 $end_date = strtotime($date2);

 $query  =  mysql_query("SELECT * FROM reservation WHERE  ('pudate'>".$start_date." AND         
 'dodate'<".$end_date.") OR ('pudate'<".$start_date." AND 'dodate'>".$end_date." ) OR 
  ('pudate'<".$end_date." AND 'dodate'>".$end_date.") OR ('pudate'<".$start_date." AND 
  'dodate'>".$start_date." )");

 $result = mysql_query($query); 

  if ($result) 

 {
   echo "vehicle is available";
 }

 else
 {
 echo "vehicle is not available";    

  }

 ?>

pudate和dodate是数据库中的日期。 当我尝试运行它总是显示输出,因为车辆不可用。我正在运行查询st检查汽车是否已经在两天之间预订,如果汽车已经预订,它会说汽车不可用,但无论我如何尝试运行查询,它总是给我相同的结果。< / p>

2 个答案:

答案 0 :(得分:0)

您未能获取查询的实际结果。实际上,您拨打mysql_query()两次(返回句柄)。在您的情况下,使用mysql_num_rows()来获取行数,这应该告诉您车辆是否可用。

答案 1 :(得分:0)

问题是您没有正确处理查询结果。要解决此问题,您可以尝试以下方法:

$result =  mysql_query("SELECT * FROM reservation WHERE  ('pudate'>".$start_date." AND         
  'dodate'<".$end_date.") OR ('pudate'<".$start_date." AND 'dodate'>".$end_date." ) OR 
  ('pudate'<".$end_date." AND 'dodate'>".$end_date.") OR ('pudate'<".$start_date." AND 
  'dodate'>".$start_date." )");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    //$row contains the results of your query  
}