0

I have an form where users can select a date range. For example, a date range could be:

$start = "5/1/2013";
$end = "6/5/2013";

I can easily grab the day difference between the two by doing something like:

$date1 = new DateTime($start);
$date2 = new DateTime($end);

$interval = $date1->diff($date2)

But if I want to build an array that would contain a monthly grouping, where each array would have the minimum and maximum date for each group. For example:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(8) "5/1/2013"
    [1]=>
    string(9) "5/30/2013"
  }
  [1]=>
  array(2) {
    [0]=>
    string(8) "6/1/2013"
    [1]=>
    string(8) "6/5/2013"
  }
}

How could I accomplish this?

etm124
  • 2,100
  • 4
  • 41
  • 77
  • This isn't a code-writing service. Show what you've tried and we'll help you get it working. – Barmar May 20 '13 at 17:57

2 Answers2

0

Use $dateX->getTimestamp(), than iterate between these two timestamps by an interval of 60*60*24*30 (one month) and use date("d/m/Y", $time) to get the formatted date.

0

Just loop through every month by using the +1 month string in the date function, and keep going until you reach the end date. You can use cal_days_in_month or the t string for date to get the number of days in each month.

Anujan
  • 928
  • 1
  • 9
  • 20