Forum Moderators: coopster

Message Too Old, No Replies

Last x amount of months

         

Crump

8:21 pm on Aug 31, 2006 (gmt 0)

10+ Year Member



I need an array that will return the numbers and years of the last x amount of months, taking in to account overlap.

Like this (assuming x = 6)

4, 2006
3, 2006
2, 2006
1, 2006
12, 2005
11, 2005

dreamcatcher

7:36 am on Sep 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Crump,

How do you want it? Do you want the months as keys or just the results in an array?


$dates = array();

for ($i=1; $i<7; $i++)
{

$previous = date("n,Y", strtotime("-$i months"));

$dates[] = $previous;

}

print_r($dates);

dc

Crump

5:30 am on Sep 16, 2006 (gmt 0)

10+ Year Member



What if I wanted to go back x number of months from a GIVEN month, instead of the CURRENT month?

dreamcatcher

7:22 am on Sep 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the mktime [zend.com] function:


$start_month = '5'; // May
$start_year = '2006'; // 2006

$dates = array();

for ($i=0; $i<6; $i++)
{

$previous = date("n,Y", mktime(0,0,0,($start_month-$i),0,$start_year));

$dates[] = $previous;

}

print_r($dates);

As May is specified as the start date above, the first month would be April. etc

dc