【问题标题】:Generate list of dates and store in database [duplicate]生成日期列表并存储在数据库中[重复]
【发布时间】:2023-03-29 06:23:02
【问题描述】:

我正在重新提出我今天早些时候提出的问题。是否可以生成日期列表,仅提供第一个和最后一个日期,然后将结果列表存储在数据库表中,每个日期占用一个新行:

示例 第 1 行:2015-08-10
第 2 行:2015-08-11
第 3 行:2015-08-12
行..:..
第 10 行:2015-08-20

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

 // current method of creatign the list of dates. 
    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($booking_from, $booking_to);

print_r($dates);

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

// table in which the dates are to be inserted 
 if (!$conn->query("INSERT INTO room_nights (bookingID, apartmentID, date_from, date_to) VALUES (LAST_INSERT_ID(), '$apartment', '$dates[0]', '". $dates[count($dates) - 1] ."')")) {
      printf("Errormessage: %s\n", $conn->error);
 }


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

 ?>

当前表格结果
date_from date_to bookingID 公寓ID
2015-08-10 2015-08-13 1 1
2015-08-17 2015-08-20 2 1

想要的结果
日期预订ID 公寓ID
2015-08-10 1 1
2015-08-11 1 1
2015-08-12 1 1

【问题讨论】:

  • 不要重复提问,尤其是在同一天。
  • 我为此道歉,但我有点绝望。 ://

标签: php date mysqli


【解决方案1】:

PHP 的 DateTime 类套件使这更容易:

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))->modify('+' . $step);
  $interval = DateInterval::createFromDateString($step);
  $period = new DatePeriod($first, $interval, $last);

  foreach ($period as $date) {
    $dates[] = $date->format($output_format);
  }

  return $dates;
}

Demo

这将创建两个DateTime() 对象,一个用于开始日期,一个用于结束日期。它还创建了一个DateInterval() 对象,它表示日期之间的步骤,而DatePeriod() 对象将它们全部存储。然后我们遍历DatePeriod() 对象并获取我们的格式化日期。

【讨论】:

  • 如何将结果保存到数据库中?
  • 这是一个完全不同的问题。只需遍历日期数组并插入每个值。
  • aaa 好的,谢谢 - 会努力解决的。
  • 已经弄明白了,但是由于某种原因,我得到了一堆 0000-00-00 而不是数据库中的实际日期。
【解决方案2】:

我对这个问题的解决方案 -

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

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


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

?>

【讨论】:

  • 回答你原来的问题,这样可以删除这个问题而不会丢失你的答案
猜你喜欢
  • 2015-11-02
  • 1970-01-01
  • 2017-05-31
  • 2016-04-02
  • 2020-05-12
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多