0
$start = new DateTime('2013-08-16');
$interval = new DateInterval('P1D');
$end = new DateTime('2013-08-20');
$end->add(new DateInterval('P1D'));
$period = new DatePeriod($start, $interval, $end);

foreach ($period as $date) {
echo $date->format('Y-m-d') . "<br />";
}

I found the above code to get all dates between two static dates.

What I would like is to get the dates between multiple $start $end pairs. The scenario is to associate the variables ($start, &end) with fields from a db in order to make multiple pairs and get the between dates of these pairs into an array.

Is it possible?

Thanks!

dimoss
  • 479
  • 1
  • 3
  • 10
  • 1
    What is your expected output array ? Can you add same in your question ? – Rikesh Dec 30 '13 at 13:16
  • `What I would like is to get the dates between multiple $start $end pairs.` - Could you clarify? – Amal Murali Dec 30 '13 at 13:20
  • Yes. I will try to explain. I have a db form where the users submit events with start and end dates. Each user can add more than one event with different start and end dates. So I want to collect all the dates for these records. For example: user_a adds event_1 with start_date = 2013-12-12 and end_date = 2013-12-13 as well as the event_2 with start_date = 2013-12-20 and end_date = 2013-12-21. The array I would like to get for this user should be: (2013-12-12, 2013-12-13, 2013-12-20, 2012-12-21). – dimoss Dec 30 '13 at 14:47
  • Just loop over the same code for different date range, and fill the results into same array. Where is the problem with that? – Glavić Dec 30 '13 at 14:48
  • The problem is how to transform the code above to get the date range for specific id's. – dimoss Dec 30 '13 at 14:51
  • Something like [this](http://pastebin.com/N3RzQptS) ? – Glavić Dec 30 '13 at 14:54

1 Answers1

0

just write the code in a function like so

function find_dates_between( $start_date, $end_date) {
$start = new DateTime($start_date);
$interval = new DateInterval('P1D');
$end = new DateTime($end_date);
$end->add(new DateInterval('P1D'));
$period = new DatePeriod($start, $interval, $end);

foreach ($period as $date) {
echo $date->format('Y-m-d') . "<br />";
}
}

After that just call the function

find_dates_between( $start_date, $end_date);

where $start_date and $end_date are extracted from you DB

Yazan Malkawi
  • 501
  • 4
  • 10