0

How can I get month Interval in php using DateInterval.

If I chose 05 Aug 2015 as start date and 17 Oct 2015 as end date, then my array output should be divided in 3 arrays:

  • 1st array for 05 Aug 2015 - 31 Aug 2015
  • 2nd array for 01 Sept 2015 - 30 Sept 2015
  • 3rd array for 01 Oct 2015 - 17 Oct 2015

We can achieve this thing by php DateInterval function?

Darragh Enright
  • 13,676
  • 7
  • 41
  • 48
Nikunj Soni
  • 854
  • 1
  • 5
  • 18

2 Answers2

1

This is pretty straightforward if you familiarise yourself with some of PHP's helpful built-in Date/Time classes.

You can use DateTime, DateInterval and DatePeriod to achieve your results. For example:

<?php

// create start/end dates and interval
$start    = new DateTime('05 Aug 2015');
$end      = new DateTime('17 Oct 2015');
$interval = new DateInterval('P1D');

// DatePeriod excludes the last day
// so bump the end date by one
$end->modify('+1 day');

// create a DatePeriod for each day in range
$period = new DatePeriod($start, $interval, $end);

$months = [];

foreach ($period as $date) {
    $months[$date->format('F')][] = $date->format('m/d/Y');
}

print_r($months);

Yields:

Array
(
    [August] => Array
        (
            [0] => 08/05/2015
            [1] => 08/06/2015
            [2] => 08/07/2015
            [3] => 08/08/2015
            [4] => 08/09/2015
            [5] => 08/10/2015
            [6] => 08/11/2015
            [7] => 08/12/2015
            [8] => 08/13/2015
            [9] => 08/14/2015
            [10] => 08/15/2015
            [11] => 08/16/2015
            [12] => 08/17/2015
            [13] => 08/18/2015
            [14] => 08/19/2015
            [15] => 08/20/2015
            [16] => 08/21/2015
            [17] => 08/22/2015
            [18] => 08/23/2015
            [19] => 08/24/2015
            [20] => 08/25/2015
            [21] => 08/26/2015
            [22] => 08/27/2015
            [23] => 08/28/2015
            [24] => 08/29/2015
            [25] => 08/30/2015
            [26] => 08/31/2015
        )

    [September] => Array
        (
            [0] => 09/01/2015
            [1] => 09/02/2015
            [2] => 09/03/2015
            [3] => 09/04/2015
            [4] => 09/05/2015
            [5] => 09/06/2015
            [6] => 09/07/2015
            [7] => 09/08/2015
            [8] => 09/09/2015
            [9] => 09/10/2015
            [10] => 09/11/2015
            [11] => 09/12/2015
            [12] => 09/13/2015
            [13] => 09/14/2015
            [14] => 09/15/2015
            [15] => 09/16/2015
            [16] => 09/17/2015
            [17] => 09/18/2015
            [18] => 09/19/2015
            [19] => 09/20/2015
            [20] => 09/21/2015
            [21] => 09/22/2015
            [22] => 09/23/2015
            [23] => 09/24/2015
            [24] => 09/25/2015
            [25] => 09/26/2015
            [26] => 09/27/2015
            [27] => 09/28/2015
            [28] => 09/29/2015
            [29] => 09/30/2015
        )

    [October] => Array
        (
            [0] => 10/01/2015
            [1] => 10/02/2015
            [2] => 10/03/2015
            [3] => 10/04/2015
            [4] => 10/05/2015
            [5] => 10/06/2015
            [6] => 10/07/2015
            [7] => 10/08/2015
            [8] => 10/09/2015
            [9] => 10/10/2015
            [10] => 10/11/2015
            [11] => 10/12/2015
            [12] => 10/13/2015
            [13] => 10/14/2015
            [14] => 10/15/2015
            [15] => 10/16/2015
            [16] => 10/17/2015
        )

)

Just to note: you specified that you wanted to break the result into three separate arrays. I assume the results will be dynamic - in which the number of months could vary - so I think you are better generating a multidimensional array of results.

In the example above I used the month names as keys. $date->format('F') returns a full textual representation of a month, such as January or March.

If you want to get a list of the months contained in the $months array you can just do:

print_r(array_keys($months));

Which returns the keys, and should give you enough information to access the data for your needs:

Array
(
    [0] => August
    [1] => September
    [2] => October
)

Hope this helps :)

Darragh Enright
  • 13,676
  • 7
  • 41
  • 48
1

you can try the below code. I am not sure but you might get what you are looking for

$interval = DateInterval::createFromDateString('1 month');
$period   = new DatePeriod($d1, $interval, $d2);

foreach ($period as $dt) {

if($dt->format("Y-m") == date("Y-m", strtotime($date1))){
 $date_array[$dt->format("Y-m")] = array(
  'start_date' => $startDate,
  'end_date' => $dt->format("Y-m-t"),
 );
}else if($dt->format("Y-m") == date("Y-m", strtotime($date2))){
 $date_array[$dt->format("Y-m")] = array(
  'start_date' => $dt->format("Y-m-").'01',
  'end_date' => $endDate,
 );
}else{
 $date_array[$dt->format("Y-m")] = array(
  'start_date' => $dt->format("Y-m-").'01',
  'end_date' => $dt->format("Y-m-t"),
 );
}
}

foreach( $date_array as $d_key=>$da ){
    /* your stuff */
}