【发布时间】: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();
}
?>
【问题讨论】: