0

I have a date_start and a date_end string and I would like to iterate on each day between the two dates.

Something like below

$dateStart = "2017-07-18";
$dateEnd = "2017-08-08";
$datesInBewteen = getDatesInBetween($dateStart, $dateEnd);
foreach ($datesInBetween as $date) {
    // do stuff
}

How should the getDatesInBetween function look like?

user10089632
  • 5,216
  • 1
  • 26
  • 34
Bogdan
  • 1,840
  • 1
  • 25
  • 39
  • A quick google. [Have you tried this?](https://stackoverflow.com/questions/4312439/php-return-all-dates-between-two-dates-in-an-array) – Matt Aug 08 '17 at 11:18
  • Read: https://stackoverflow.com/questions/14851190/date-function-to-display-all-dates-between-two-dates – Pawan Thakur Aug 08 '17 at 11:21
  • @Matt I've searched stackoverflow and found nothing before asking my question - I try not to use google (search yourserf for "iterate between two dates with PHP" on stackoverflow) – Bogdan Aug 08 '17 at 11:26
  • @Bogdan.P I understand but google is your friend to find stack answers. I googled your exact Topic and the top 2 answers were stack, both with the exact answer you need :). Don't always need a new question when answers are out there and easy to find! – Matt Aug 08 '17 at 11:29
  • @Matt thanks for your advice – Bogdan Aug 08 '17 at 11:30

3 Answers3

5

Here is the working code for you: https://eval.in/842849

You should use DatePeriod which takes start-date, date interval, and end-date as arguments.

You will get the result object, which you can loop thru to get the desired dates between the 2 dates:

<?php
$begin = new DateTime('2017-07-18');
$end = new DateTime('2017-08-08');

$daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);

foreach($daterange as $date){
    echo $date->format("Y-m-d") . "\n";
}

?>
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
  • Code dumps do not make for good answers. You should explain *how* and *why* this solves their problem. I recommend reading, "[How do I write a good answer?"](http://stackoverflow.com/help/how-to-answer) – John Conde Aug 08 '17 at 11:22
  • I mean, I thought its self-explanatory. Getting the `DatePeriod` and incrementing by 1D till the `$end` – Milan Chheda Aug 08 '17 at 11:23
  • 1
    Using DatePeriod and DateInterval is not self-explanatory. What does `P1D` mean? What is a DateInterval ? DatePeriod? This code is the best answer but it is not self-explanatory. – John Conde Aug 08 '17 at 11:27
1

You can use while loop to iterate between two dates.

$dateStart = "2017-07-18";
$dateEnd = "2017-08-08";
$current_date = $dateStart;
while(strtotime($current_date) < strtotime($dateEnd))
{
  echo $current_date."<br>";
  $current_date= date("Y-m-d",strtotime("+1 day",strtotime($current_date)));
}
B. Desai
  • 16,414
  • 5
  • 26
  • 47
1

Try below code, running Example

<?php

    // Start date
    $date = '2017-07-18';
    // End date
    $end_date = '2017-08-08';

    while (strtotime($date) <= strtotime($end_date)) { // Compare start date is less than end date
             echo      "$date\n";
              $date = date ("Y-m-d", strtotime("+1 day", strtotime($date))); // increment date by 1 day
    }

?>

With the help of while loop you can check each day and inside that increase each day

John Conde
  • 217,595
  • 99
  • 455
  • 496
Narayan
  • 1,670
  • 1
  • 19
  • 37