生成2个日期之间的日期列表,并在数据库中保存列表

时间:2015-08-10 10:14:04

标签: php dynamic mysqli insert

我想对我目前遇到的问题提供一些帮助。我需要使用列表中的第一个和最后一个日期生成日期列表,然后保存在数据库表中生成的日期列表。这样做的最佳方式是什么?

到目前为止我的代码:

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

?>

2 个答案:

答案 0 :(得分:0)

出现此错误的原因是您调用函数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 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是一个沙箱,您可以在其中查看结果。

请注意,$ 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] ."')");

P.S。此外,我看到您将$booking_from$booking_to参数从字符串转换为日期 - 一次在daterange函数定义之前,一次在同一函数中。

答案 1 :(得分:0)

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

  

步骤1:创建日期为
的数组   步骤2:遍历数组并在数据库表中插入日期;

<CollectionViewSource x:Key="ColoursSource"  x:Shared="False"> 
    <CollectionViewSource.Source>
      <ObjectDataProvider 
        MethodName="GetValues" 
        ObjectType="{x:Type system:Enum}">
        <ObjectDataProvider.MethodParameters>
          <x:Type TypeName="entities:Colours" />
        </ObjectDataProvider.MethodParameters>
      </ObjectDataProvider>
    </CollectionViewSource.Source>
  </CollectionViewSource>