2012-08-15 266 views
0

我想了解兩個日期之間有多少天的代碼波紋管是我所說的一個例子,但它不工作所有我在數據庫中得到的是0計算日期之間的天數

 <?php 
    $hotel_name = $_POST['hotelName']; 
    $room_type = $_POST['roomType']; 
    //date From value 
    $dayfrom = $_POST['dayfrom']; 
    $monthfrom = $_POST['monthfrom']; 
    $yearfrom = $_POST['yearfrom']; 
    //date To value 
    $dayto  = $_POST['dayto']; 
    $monthto = $_POST['monthto']; 
    $yearto  = $_POST['yearto']; 
    $arivDate = sprintf('%04d-%02d-%02d', $yearfrom, $monthfrom, $dayfrom); 
    $depDate = sprintf('%04d-%02d-%02d', $yearto, $monthto, $dayto); 
    $child = $_POST['child']; 
    $adult = $_POST['adult']; 
    $days = $arivDate - $depDate; 
    //$sortedfromdate = strtotime($arivDate); 
    //$sortedtodate = strtotime($depDate); 
    $query = "INSERT INTO tempbooking(book_date, Ariv_date, dep_date, hotel_name 
      )VALUES(
      CURDATE(), '{$arivDate}', '{$depDate}', '{$hotel_name}')"; 
      if(mysql_query($query,$connection)){ 
         $booking_id = mysql_insert_id(); 
      }else{ 
      die("The Booking was not successful". mysql_error()); 
      } 
         $queryres="INSERT INTO ro_reservation(booking_id, children, adult, room_type, number_days 
           )VALUES(
            {$booking_id}, {$child}, {$adult}, '{$room_type}',{$days})"; 
         if(mysql_query($queryres,$connection)){ 
         header("Location:index.php"); 
         //echo"<a href=\"index.php\"> Reservation Inserted</a>"; 
         }else{ 
         echo "Nothing was inserted in to the ro_reservation"; 
         }   
?> 
+2

[SQL Injection](http://en.wikipedia.org/wiki/SQL_injection)alert:[防止SQL注入的最佳方法](http://stackoverflow.com/questions/60174/)最好的方式對防止-SQL注入式的PHP)。 – PeeHaa 2012-08-15 18:17:36

+0

您正在減去字符串,而不是日期/時間值。 – 2012-08-15 18:35:26

回答

1
$arivDate = strtotime("2012-01-01"); 
$depDate = strtotime("2012-08-14"); 
$datediff = abs($arivDate - $depDate) 
echo floor($datediff/(60*60*24)); 
+0

但我有兩個時間爲什麼我需要$現在 – 2012-08-15 18:23:10

+0

謝謝它的工作。但我仍然在減去即使未來的日期,例如1-9-2012至29-10-2012它給我-30而不是30.你能告訴我什麼是缺失 – 2012-08-15 18:31:27

+0

已經編輯了答案。請現在嘗試 – 2012-08-15 18:45:37

相關問題