计算日期之间的天数

时间:2012-08-15 18:15:28

标签: php mysql

我试图找出两个日期之间有多少天,下面的代码是我所说的一个例子,但它不能用于我在数据库中获得的所有内容是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";
                        }        
?>

1 个答案:

答案 0 :(得分:1)

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