0

I have a start date of 20090101 and an end date of 20091130 and I'm trying to build and array of all the months in between, which would look like this:

<?php

/* ... */

$arrDates['Jan'] = 2009;
$arrDates['Feb'] = 2009;
$arrDates['Mar'] = 2009;

/* ... */

?>

How can I do this?

Jake McGraw
  • 55,558
  • 10
  • 50
  • 63
Oliver
  • 809
  • 1
  • 11
  • 18
  • 3
    Please explain better your array structure. Why the key is the month and the value the year ('Jan' and 2009 in your example: $arrDates ['Jan'] = 2009;). What would be the structure if the end date is more than a full year after the start date? – danii Nov 20 '09 at 12:49

6 Answers6

1

I don't fully understand your array structure.

But maybe this helps: When using PHP 5.3 you can use code like below to get an iterator with all months in the given range:

<?php
$db = new DateTime( '2009-01-01 00:00:00' );
$de = new DateTime( '2009-11-30 23:59:59' );
$di = DateInterval::createFromDateString( 'first day of next month' );

foreach ( $di as $dt )
{
    echo $dt->format( "Y-m\n" );
}
?>
johannes
  • 15,807
  • 3
  • 44
  • 57
1

The following snippet creates such an array:

$startDate = '20090101';
$endDate = '20091130';
$arrDates = array();

$cur = strtotime($startDate);
$end = strtotime($endDate);
while ($cur < $end) {
    $arrDates[date('M', $cur)] = date('Y', $cur);
    $cur = mktime(0, 0, 0, date('m', $cur) + 1, 1, date('Y', $cur));
}

// If you want to add the 'end' month too...
$arrDates[date('M', $end)] = date('Y', $end);

However, note that, as danii commented, you are unclear about how you want to handle a timespan that is larger than a year. The code above will simply use the last year in the range you provide.

This code will work with pretty much any version of PHP (PHP 4+). If you want a more elegant solution and are using PHP 5.2+, I recommend the solution offered by GZipp.

John
  • 691
  • 3
  • 8
0

I had a similar situation for a website i was building for travelagency. You need timestamps, arrays and looping. Take a look at the date function PHP provides. It gives you some interesting options to play with dates. E.g. the no. of days in a specified month.

Ben Fransen
  • 10,884
  • 18
  • 76
  • 129
0

You say "the months in between", but since your example includes the starting month, I assume you mean "the months in between plus the starting and ending months".

$dt_start = new DateTime('20090101');
$dt_end   = new DateTime('20091130');
$arrDates[] = $dt_start->format('M');
while ($dt_start->modify('first day of next month') <= $dt_end) {
    $arrDates[] = $dt_start->format('M');  // Or whatever you want to do with it.
}

(This is essentially johannes' solution with a little manual reading applied to adapt it for PHP 5.2.)

GZipp
  • 5,386
  • 1
  • 22
  • 18
0

You can't use the month as a key, the key must be unique or if your range spans more than a year it won't work correctly. This will return an array with Mon-Year as the key

function foo($startdate, $enddate) {

    // create a timestamp for start date
    if(!preg_match('/^(\d{4})(\d{2})(\d{2})$/', $startdate, $m)) die('Invalid start date format');
    $start_time = mktime(0, 0, 0, $m[2], $m[3], $m[1]);

    // create a timestamp for end date
    if(!preg_match('/^(\d{4})(\d{2})(\d{2})$/', $enddate, $m)) die('Invalid end date format');
    $end_time = mktime(23, 59, 59, $m[2], $m[3], $m[1]);

    // build the array of months by incrementing $start_time by one month through each iteration
    $ret = array();
    while($start_time < $end_time) {
        $ret[date('M-Y', $start_time)] = date('Y', $start_time);
        $start_time = strtotime(date('m/d/Y', $start_time).' +1month');
    }

    return $ret;
}

$arrDates = foo('20090101', '20111130');
print_r($arrDates);

Array(
    [Jan-2009] => 2009
    [Feb-2009] => 2009
    [Mar-2009] => 2009
    [Apr-2009] => 2009
    [May-2009] => 2009
    [Jun-2009] => 2009
    [Jul-2009] => 2009
    [Aug-2009] => 2009
    ....
)
Rob
  • 8,042
  • 3
  • 35
  • 37
-1

A bit convoluted but works...:

function buildDateRange($strStartDate, $strEndDate) 
{                   

    $strStartM = date('M', $strStartDate);
    $strStartY = date('Y', $strStartDate);

    $strEndM = date('M', $strEndDate);
    $strEndY = date('Y', $strEndDate);

    $intCurMN = date('m', $strStartDate);       

    $ii = 0;
    $blnFinished = FALSE;

    while(!$blnFinished) 
    {

        $strCurM  = date('M', mktime(0, 0, 0, $intCurMN , "01", $strStartY));                   
        $strCurY  = date('Y' ,mktime(0, 0, 0, $intCurMN , "01", $strStartY));                                       

        $arrSearchDates [$strCurM] = $strCurY;

        $intCurMN = date('m', mktime(0, 0, 0, $intCurMN+1 , "01", $strStartY));                 


        if($strEndM == $strCurM && $strEndY == $strCurY) 
        {
            $blnFinished = TRUE;    
        }
    }

    Return ($arrSearchDates);

}
Oliver
  • 809
  • 1
  • 11
  • 18
  • All arguments to mktime should be int. Same for the second argument to the date function. Also, your solution doesn't work when the range spans more than 1 year (for example 20091201-20100201). – John Nov 20 '09 at 17:21