生成两个日期之间的日期列表并将列表保存在数据库中
generate list of dates between 2 dates and save list in database
我需要一些帮助来解决我目前遇到的问题。我需要使用列表中的第一个和最后一个日期生成日期列表,然后将生成的日期列表保存在数据库 table 中。执行此操作的最佳方法是什么?
到目前为止我的代码:
<?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();
}
?>
出现此错误的原因是您调用函数 daterange
而未保存它 returns 的结果。所以不要这样称呼它:
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);
?>
returns 存储在 $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 字段中。即使你想存储一个Array,你也应该使用PHP的serialize
函数并将它存储在数据库中的一个BLOB字段中。 (See the documentation)
因此,如果您想存储日期范围,您将需要在数据库中至少有两个字段,例如date_from
和 date_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
函数定义之前,一次在同一个函数中。
这是我对这个问题的解答。
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();
?>
我需要一些帮助来解决我目前遇到的问题。我需要使用列表中的第一个和最后一个日期生成日期列表,然后将生成的日期列表保存在数据库 table 中。执行此操作的最佳方法是什么?
到目前为止我的代码:
<?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();
}
?>
出现此错误的原因是您调用函数 daterange
而未保存它 returns 的结果。所以不要这样称呼它:
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);
?>
returns 存储在 $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 字段中。即使你想存储一个Array,你也应该使用PHP的serialize
函数并将它存储在数据库中的一个BLOB字段中。 (See the documentation)
因此,如果您想存储日期范围,您将需要在数据库中至少有两个字段,例如date_from
和 date_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
函数定义之前,一次在同一个函数中。
这是我对这个问题的解答。
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();
?>