generate list of dates between 2 dates and save list in database

前端 未结 2 990
失恋的感觉
失恋的感觉 2021-01-22 09:28

I would like a bit of help with an issue I\'m currently having. I need to generate a list of dates using the first and last date in the list and then save the list of dates gen

相关标签:
2条回答
  • 2021-01-22 10:20

    The reason to get this error is that you call the function daterange without saving the result that it returns. So instead of calling it this way:

    daterange($booking_from, $booking_to, $step = '+1 day', $output_format = 'y-m-d');
    

    You should add the $dates variable to store the returned result of the function and to use it afterwards.

    $dates = daterange($booking_from, $booking_to, $step = '+1 day', $output_format = 'y-m-d');
    

    EDIT:

    The following code:

    <?php    
    function daterange($booking_from, $booking_to, $step = '+1 day', $output_format = 'Y-m-d') {
          $dates = array();
          $first = strtotime($booking_from);
          $last = strtotime($booking_to);
    
          while ($first <= $last) {
            $dates[] = date($output_format, $first);
            $first = strtotime($step, $first);
          }
    
          return $dates;
        }
    
        $dates = daterange('2015-08-01', '2015-08-10');
    
        print_r($dates);
    ?>
    

    returns an array of dates which is stored in the $dates param:

    Array
    (
        [0] => 2015-08-01
        [1] => 2015-08-02
        [2] => 2015-08-03
        [3] => 2015-08-04
        [4] => 2015-08-05
        [5] => 2015-08-06
        [6] => 2015-08-07
        [7] => 2015-08-08
        [8] => 2015-08-09
        [9] => 2015-08-10
    )
    

    Here is a sandbox where you can see the result.

    Please take a note here that the $output_format shold be 'Y-m-d' in order to get the date in yyyy-mm-dd format.

    You can not store an Array into a DATE field. Even if you want to store an Array, you should use the PHP's serialize function and store it in a BLOB field in the database. (See the documentation)

    So if you want to store the date range you are going to need at least two fields in the DB, e.g. date_from and date_to. In this case the INSERT statement should look like this:

    $conn->query("INSERT INTO room_nights (bookingID, apartmentID, date_from, date_to) VALUES (LAST_INSERT_ID(), '$apartment', '$dates[0]', '". $dates[count($dates) - 1] ."')");
    

    P.s. Also I saw that you are converting the $booking_from and $booking_to params multiple times from string to date - once before the daterange function definition and once in the very same function.

    0 讨论(0)
  • 2021-01-22 10:31

    Here is my solution to the question.

    Step1: Create an array with the dates
    Step2: Loop through the array and insert the dates in the database table;

    <?php
    $day_from = (isset($_POST['day_from']) ? $_POST['day_from'] : null);
    $month_from = (isset($_POST['month_from']) ? $_POST['month_from'] : null);
    $year_from = (isset($_POST['year_from']) ? $_POST['year_from'] : null);
    $booking_from = $year_from."-".$month_from."-".$day_from;
    $day_to = (isset($_POST['day_to']) ? $_POST['day_to'] : null);
    $month_to = (isset($_POST['month_to']) ? $_POST['month_to'] : null);
    $year_to = (isset($_POST['year_to']) ? $_POST['year_to'] : null);
    $booking_to = $year_to."-".$month_to."-".$day_to;
    $no_of_nights = abs(strtotime($booking_to) - strtotime($booking_from));     
    $days = floor($no_of_nights / (60*60*24));
    
    // create array with dates 
    function daterange($booking_from, $booking_to, $step = '+1 day', $output_format = 'Y-m-d') {
     $dates = array();
     $first = new DateTime($booking_from);
     $last = new DateTime($booking_to);
     $interval = DateInterval::createFromDateString($step);
     $period = new DatePeriod($first, $interval, $last);
    
     foreach ($period as $date) {
      $dates[] = $date->format($output_format);
      }
    
       return $dates;
     }
    
     $dates = daterange($booking_from, $booking_to);
    
      print_r($dates);
    
      include 'connect.php';
    
      if (!$conn->autocommit(FALSE)) {
      printf("Errormessage: %s\n", $conn->error);
      }
    
      // loop thru dates and save in database table
      foreach ($dates as $date) {
      if (!$conn->query("INSERT INTO room_nights (bookingID, apartmentID, dates) VALUES (LAST_INSERT_ID(), '$apartment', '$date')")) {
      printf("Errormessage: %s\n", $conn->error);
     }
       }
    
     if (!$conn->commit()) {
     printf("Errormessage: %s\n", $conn->error);
    }
    $conn->close();
    
     ?>
    
    0 讨论(0)
提交回复
热议问题