【问题标题】:generate list of dates between 2 dates and save list in database生成两个日期之间的日期列表并将列表保存在数据库中
【发布时间】:2015-11-02 05:06:32
【问题描述】:

对于我目前遇到的问题,我需要一些帮助。我需要使用列表中的第一个和最后一个日期生成日期列表,然后将生成的日期列表保存在数据库表中。最好的方法是什么?

到目前为止我的代码:

<?php

    $apartment = (isset($_POST['apartment']) ? $_POST['apartment'] : null);
    $name = (isset($_POST['name']) ? $_POST['name'] : null);
    $surname = (isset($_POST['surname']) ? $_POST['surname'] : null);
    $email = (isset($_POST['email']) ? $_POST['email'] : null);
    $address = (isset($_POST['address']) ? $_POST['address'] : null);
    $mobile = (isset($_POST['mobile']) ? $_POST['mobile'] : null);
    $pax = (isset($_POST['pax']) ? $_POST['pax'] : null);
    $address = (isset($_POST['address']) ? $_POST['address'] : null);
    $remarks = (isset($_POST['remarks']) ? $_POST['remarks'] : null);
    $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));

   include 'connect.php';

 if (!$conn->autocommit(FALSE)) {
    printf("Errormessage: %s\n", $conn->error);
 }

 if (!$conn->query("INSERT INTO client_details (clientID, name, email, address, mobile) VALUES ('', '$name $surname', '$email', '$address', '$mobile')")) {
     printf("Errormessage: %s\n", $conn->error);
 }

 if (!$conn->query("INSERT INTO bookings (bookingID, apartmentID, clientID, date_from, date_to, nights, pax, remarks) VALUES ('', '$apartment', LAST_INSERT_ID(), '$booking_from', '$booking_to', '$days', '$pax', '$remarks')")) {
     printf("Errormessage: %s\n", $conn->error);
 }

function dateArray($booking_from, $booking_to) {
    echo "yo";

    $aryRange = array();

     $iDateFrom=mktime(1,0,0,substr($booking_from,5,2),     substr($booking_from,8,2),substr($booking_from,0,4));
    $iDateTo=mktime(1,0,0,substr($booking_to,5,2),     substr($booking_to,8,2),substr($booking_to,0,4));

if ($iDateTo>=$iDateFrom) {
    array_push($aryRange, date('Y-m-d', $iDateFrom));
    {
        while ($iDateFrom<$iDateTo)
        {
            $iDateFrom+=86400; // add 24 hours
        array_push($aryRange,date('Y-m-d',$iDateFrom));
        }
    }
    return $aryRange;
    }



dateArray($booking_from, $booking_to);

  if (!$conn->query("INSERT INTO room_nights (bookingID, apartmentID, dates) VALUES (LAST_INSERT_ID(), '$apartment', '$dates['dates']')")) {
     printf("Errormessage: %s\n", $conn->error);
 }


 if (!$conn->commit()) {
     printf("Errormessage: %s\n", $conn->error);
 }
 $conn->close();
}

?>

【问题讨论】:

    标签: php dynamic mysqli insert


    【解决方案1】:

    出现此错误的原因是您调用函数daterange 而不保存它返回的结果。所以不要这样称呼它:

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

    您应该添加$dates 变量来存储函数的返回结果并在以后使用它。

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

    编辑:

    以下代码:

    <?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);
    ?>
    

    返回存储在$dates 参数中的日期数组:

    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 是一个沙盒,您可以在其中查看结果。

    请注意,$output_format 应为'Y-m-d',以便获取yyyy-mm-dd 格式的日期。

    您不能将数组存储到 DATE 字段中。即使你想存储一个数组,你也应该使用 PHP 的serialize 函数并将它存储在数据库的一个 BLOB 字段中。 (See the documentation)

    因此,如果您想存储日期范围,您将需要在数据库中至少有两个字段,例如date_fromdate_to。在这种情况下,INSERT 语句应如下所示:

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

    附:我还看到您将 $booking_from$booking_to 参数从字符串多次转换为日期 - 一次在 daterange 函数定义之前,一次在同一个函数中。

    【讨论】:

    • 试过了 - 但是我收到一条错误消息,指出注意:数组到字符串的转换在 C:\xampp\htdocs\WebDevelopment\V18\insertdata.php 第 52 行(在 sql 中)
    • 看来问题不止一个。首先我想指出,没有必要传递$step$output_format 变量,因为它们与函数中的默认变量(php.net/manual/en/…)相同。之后,在 daterange 函数中,您将创建并返回一个您尝试插入到表中的数组。这就是您收到此通知的原因。数据库中的字段dates 是什么数据类型,您实际上要在其中插入什么?
    • 日期的数据类型是日期,即yyyy-mm-dd。我想插入数组中生成的日期范围。
    • 感谢您的帮助,但表格中的结果与预期/期望的不符。我希望存储所有日期,而不仅仅是第一个和最后一个。
    【解决方案2】:

    这是我对这个问题的解决方案。

    第 1 步:创建一个包含日期的数组
    Step2:遍历数组,在数据库表中插入日期;

    <?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();
    
     ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 2023-03-29
      • 2010-10-05
      • 1970-01-01
      相关资源
      最近更新 更多