在数据库中生成2个日期和保存列表之间的日期列表 [英] generate list of dates between 2 dates and save list in database

查看:111
本文介绍了在数据库中生成2个日期和保存列表之间的日期列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的代码到目前为止:

 <?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\\\
,$ conn-> error);
}

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

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

函数dateArray($ booking_from,$ booking_to){
echoyo;

$ 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\\\
,$ conn-> error);
}


如果(!$ conn-> commit()){
printf(Errormessage:%s\\\
,$ conn-> ;错误);
}
$ conn-> close();
}

?>


解决方案

这是我的解决方案。


步骤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));

//创建数组与日期
函数daterange($ booking_from,$ booking_to,$ step ='+1 day',$ output_format ='Ym-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);
}

返回$日期;
}

$ dates = daterange($ booking_from,$ booking_to);

print_r($ dates);

include'connect.php';

if(!$ conn-> autocommit(FALSE)){
printf(Errormessage:%s\\\
,$ 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\\\
,$ conn- >错误);
}
}

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

?>


I would like a bit of help with an issue I'm currently having. I need to generate a list of dates using the first and last date in the list and then save the list of dates generated in a database table. What is the best way to do this?

My code so far:

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

?>

解决方案

Here is my solution to the question.

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

 ?>

这篇关于在数据库中生成2个日期和保存列表之间的日期列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆