0

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);
?>
3
  • Your function getDateRange() is working. So, why don't you call that function inside while loop? Commented Jun 7, 2017 at 18:20
  • @NanaPartykar Thanks for the reply edited the script calling the function inside the loop i was expecting to get an array with the dates of all rows of the database but im getting only the dates of the last row any idea what im doing wrong? Commented Jun 7, 2017 at 18:28
  • Sorry for late reply @marjin You did correct thing, just missed to print for each loop. Below is my answer. Please let me know, If I missed anything. Commented Jun 7, 2017 at 18:43

2 Answers 2

1

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

.

.
.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks i only need to get them in 1 array now but i think i will get it
0

You can use DatePeriod class.

Your code would something like this:

<?php
$rows = [
    [ new DateTime(), (new DateTime())->modify('+3 days') ],
    [ (new DateTime())->modify('+20 days'), (new DateTime())->modify('+30 days') ],
];

$dates = [];

foreach ($rows as $row) {
    $dateRange = new DatePeriod($row[0], new DateInterval('P1D'), $row[1]);

    foreach ($dateRange as $date) {
        $dates[] = $date;
    }
}

var_dump($dates);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.