0

I am trying to find out how many days between two date the code bellow is an example of what i am saying but it does not work all what i get in the database is 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: [Best way to prevent SQL Injection in PHP](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php). – PeeHaa Aug 15 '12 at 18:17
  • You're subtracting STRINGS, not date/time values. – Marc B Aug 15 '12 at 18:35

1 Answers1

1
 $arivDate = strtotime("2012-01-01");
 $depDate = strtotime("2012-08-14");
 $datediff = abs($arivDate - $depDate)
 echo floor($datediff/(60*60*24));
Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61