php 收集 2 个日期之间的日期并为每个日期创建条目

php collect dates between 2 dates and create entry for each

我已经搜索了一段时间,但无法收集足够的信息来解决这个问题。

我创建了一个活动日历,其中包含一个允许用户输入活动预订范围的字段。我已经为所选日期配置了开始日期。当用户输入结束日期时,字段中的信息将为范围内的每个日期重复。

            $requester = $_POST['txtrequester'];
            $title = $_POST['txttitle'];
            $detail = $_POST['txtdetail'];
            $startTime = $_POST['txtstarttime'];
            $endTime = $_POST['txtendtime'];
            $endDate = $_POST['txtenddate']; //end date of range request
            $eventdate = $month."/".$day."/".$year;
            $today_dt = $year."-".$month."-".$day;
            $startDate_dt = strtotime("$year."-".$month."-".$day");
            $endDate_dt = strtotime($endDate);
            $endDate_dt = $endDate_dt->modify('+1 day');
            $interval = DateInterval::createFromDateString('1 day');
            $period = new DatePeriod($today_dt, $interval, $endDate_dt);

谁能帮我解决这个问题?提前致谢

--编辑--

代码将事件表单数据插入 mysql

        $sqlinsert = "INSERT INTO conferenceevents (Requester,Title,Detail,eventDate,dateAdded,startTime,endTime,endRange) values ('$requester','$title','$detail','$eventdate',now(),'$startTime','$endTime','$endDate')";
        $resultinsert = $connection->query($sqlinsert); 


        if($resultinsert){
            echo "<h4>Event was Successfully Added</h4><br>";
        }else{
            echo "<h4>Event Failed to be Added</h4><br>" . $sqlinsert;
        }
    }

活动形式html

<form name='eventform' method='POST' action="<?php $_SERVER['PHP_SELF']; ?>?month=<?php echo $month;?>&day=<?php echo $day;?>&year=<?php echo $year;?>&v=true&add=true">

    <div class="form-group">
    <label for="requester">Requester</label>
    <select class="form-control" name='txtrequester'>
      <option name='txtrequester'>Melissa</option>
      <option name='txtrequester'>Greg</option>
      <option name='txtrequester'>Matt</option>
      <option name='txtrequester'>Michael</option>
      <option name='txtrequester'>Ben</option>
      <option name='txtrequester'>Pat</option>
      <option name='txtrequester'>Maria</option>
    </select>
    </div>
    <div class="form-group">
    <label for="eventTitle">Event Title</label>
    <input type="text" class="form-control" id="eventTitle" placeholder="Event Title" name='txttitle'>
    </div>
    <div class="form-group">
    <label for="eventStartTime">Event Start Time</label>
    <input type="time" class="form-control" id="eventStartTime" value="12:00" name='txtstarttime'>
    </div>
    <div class="form-group">
    <label for="eventEndTime">Event End Time</label>
    <input type="time" class="form-control" id="eventEndTime"  value="13:00" name='txtendtime'>
    </div>
    <a href class="option"><h4>Click here to enter date range (optional)</h4></a>
    <div class="form-group range" style="display: none;">
    <label for="eventStartDate">Event Start Date</label>
    <input type="text" class="form-control" id="disabledInput" name='txtstartdate' disabled value="<?php echo $month . "/" . $day . "/" . $year; ?>">
    </div>
    <div class="form-group range" style="display: none;">
    <label for="eventEndDate">Event End Date</label>
    <input type="date" value="<?php echo $year . "-" . $month . "-" . $day; ?>" class="form-control" id="eventEndDate" name='txtenddate'>
    </div>
    <div class="form-group">
    <label for="eventDetail">Event Detail</label>
    <input type="text" id="eventDetail" type="text" class="form-control" placeholder="Event Detail" name='txtdetail'>
    </div>
    <button type="submit" class="btn btn-default" name='btnadd'>Add Event</button>

</form>

您似乎正试图为给定日期范围内的每个日期在数据库中插入一行...请像这样尝试

$startTime = strtotime( '2015-01-11' );
$endTime = strtotime( '2015-01-20' );

// Loop between timestamps, 24 hours at a time
for ( $i = $startTime; $i <= $endTime; $i = $i + 86400 ) {
            $given_date =  date( 'Y-m-d', $i )."<br>";          
            // You can put your database insert query here
              echo "Data for $given_date is inserted";
}

更有效的插入多行的方法如下..

$startTime = strtotime( '2015-01-11' );
$endTime = strtotime( '2015-01-20' );


 $sqlinsert = "INSERT INTO conferenceevents (Requester,Title,Detail,eventDate,dateAdded,startTime,endTime,endRange) values ";
 // Loop between timestamps, 24 hours at a time
    for ( $i = $startTime; $i <= $endTime; $i = $i + 86400 ) {
            $eventdate =date('Y-m-d', $i ); 
          // Constructing the insert query
         $sqlinsert .= " ('$requester','$title','$detail','$eventdate',now(),'$startTime','$endTime','$endDate'),";
}

 $bulk_insert_query = rtrim($sqlinsert, ","); // to remove last comma
 $resultinsert = $connection->query($bulk_insert_query); 

更多地了解批量插入查询尝试回显 $bulk_insert_query 并查看它是如何构建的以及您希望如何改进..