tags:

views:

930

answers:

5
for($x=0; $x<12; $x++)
{
    $month = mktime(0, 0, 0, date("m")+$x, date("d"),  date("Y"));
    $key = date('m', $month);
    $monthname = date('F', $month);
    $months[$key] = $monthname;
}

I know for sure I'm doing the math incorrectly for the 4th parameter of mktime. I'm starting with the current month number ( 7 being July ) and adding 1 for each next month, sometimes it ends up being that the same month is returned twice, maybe because I'm not setting it to the beginning of the month? How would you improve/recode this?

Result is that $months would result in an array where 07 = July 08 = August, 09 = September. Right now it populates October twice. I think it has to do with today being the 31st and it incorrectly adds and reaches the next month.

+1  A: 

Given 2592000 is 30 days.

$month_time = 60*60*24*30; // 30 Days
for($x=0; x<12; $x++)
{
     $time = time()+($month_time*$x);
     $key = date('m', $time);
     $month[$key] = date('F', $time);
}

In an answer on StackOverflow, can't find it right now, someone compared the performance of multiple methods of creating a time 1 week from now. Directly using numbers was much more efficient than any other method.

Chacha102
You way seems to show the same month twice, if I start on Jan 1, then 30 days would still be January.It may be better to go 32 days at a time then the only problem time is starting toward the end of January, as you may skip over Feb.
James Black
how would you do it? would it be more accurate but less efficient using strtotime?
meder
"Performance of creating a time one week from now" is all fine and good, with months you need to consider a few more details. :)
deceze
+5  A: 

Just fixed your code slightly, this should work pretty well:

$months = array();
$currentMonth = (int)date('m');

for($x = $currentMonth; $x < $currentMonth+12; $x++) {
    $months[] = date('F', mktime(0, 0, 0, $x, 1));
}

Note that I took out the array key, as I think it's unnecessary, but you can change that of course if you need it.

deceze
+1  A: 

An alternative would be to use strtotime:

for ($x=0; $x < 12; $x++) {

    $time = strtotime('+' . $x . ' months', strtotime(date('Y-M' . '-01')));
    $key = date('m', $time);
    $name = date('F', $time);
    $months[$key] = $name;

}

In my opinion this code is easier to read.

kbeyer
`echo date('Y-m-d', strtotime("+1 month", strtotime('2009-01-31')));` produces "2009-03-03". You'll need to at least reset the starting date to the first of the month before you can use relative time addition.
deceze
Hmm. Did not know this. So the code should be:for ($x=0; $x < 12; $x++) { $time = strtotime('+' . $x . ' months', strtotime(date('Y-M' . '-01'))); $key = date('m', $time); $name = date('F', $time); $months[$key] = $name;}instead.
kbeyer
A: 

You might be getting the last day of the month (the 31st) bug - which led to two months with the same link - that Eddy very nicely figured out for me with this answer:

 $current_month = date('n');
$MONTHS = array();
for ($m=0; $m<12; $m++) {
  $display_month = $m + $current_month;
  $MONTHS[] = date('F',mktime(1,1,1,$display_month,1,date("Y")));
songdogtech
A: 

"Result is that $months would result in an array where 07 = July 08 = August, 09 = September."

for ($key = 1; $key <=12; $key++) {
    $months[str_pad($key, 2, '0', STR_PAD_LEFT)] = date('F', strtotime('2000-' . $key));
}

If you're okay with 7 = July 8 = August, 9 = September, then:

for ($key = 1; $key <=12; $key++) {
    $months[$key] = date('F', strtotime('2000-' . $key));
}
GZipp