dongruo4601 2017-06-07 18:14
浏览 22
已采纳

从一个数组中的每一行获取两个日期之间的所有日期的数组

I'm trying to get an array of all dates between the 2 dates which im getting out the database for each row. for example there is 3 rows with 2017-02-02 and 2017-02-03 and a row with 2017-02-18 and 2017-02-20 I would like to get an array with the output [2017-02-02, 2017-02-03, 2017-02-18, 2017-02-19, 2017-02-20]

So far I have a query which gets all of the dates and echo's the start and end date and a script to get an array with the dates between 2 dates.

How can I make it work so it's getting all the dates of every row into one array?

<?php
    $result = mysqli_query($con, "SELECT * FROM invoice_line WHERE car_car_id = $id");
    while ($auto = mysqli_fetch_array($result)) {
    ?>
          <h2 class="title_car">
            <?php echo $auto['start_date'] . ' - ' . $auto['end_date'];?>
          </h2>
        <hr>
        <?php $dateRange = getDateRange($auto['start_date'], $auto['end_date']); ?>
    <?php } ?>

    <?php
    function getDateRange($startDate, $endDate, $format="Y-m-d")
{
    //Create output variable
    $datesArray = array();
    //Calculate number of days in the range
    $total_days = round(abs(strtotime($endDate) - strtotime($startDate)) / 86400, 0) + 1;
    //Populate array of weekdays and counts
    for($day=0; $day<$total_days; $day++)
    {
        $datesArray[] = date($format, strtotime("{$startDate} + {$day} days"));
    }
    //Return results array
    return $datesArray;
}

print_r($dateRange);
?>
  • 写回答

2条回答 默认 最新

  • dphdh395195 2017-06-07 18:42
    关注

    You have to foreach every array to print the dates.

    <?php
    $result = mysqli_query($con, "SELECT * FROM invoice_line WHERE car_car_id = $id");
    while ($auto = mysqli_fetch_array($result)) {?>
      <h2 class="title_car">
        <?php echo $auto['start_date'] . ' - ' . $auto['end_date'];?>
      </h2>
      <hr>
      <?php 
      $date_text = "";
      $dateRange = getDateRange($auto['start_date'], $auto['end_date']);
      if(!empty($dateRange)){
        foreach($dateRange as $dateR){
          $date_text .= $dateR.", ";
        }
      }
      echo rtrim($date_text,", ")."<br>";
    }?>
    

    Output:

    2011-05-03, 2011-05-04, 2011-05-05, 2011-05-06, 2011-05-07, 2011-05-08
    
    2011-05-20, 2011-05-21, 2011-05-22, 2011-05-23
    
    .
    
    .
    .
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)