Jump to content

Recommended Posts

The best way is to make an array of the months, imo. You could possible use strtotime but that would be way slower.

 

for ($i=0; $i<12; $i++) {
    echo date('F', strtotime("+{$i} month")) . ", ";
}

 

Untested, but should work. But as I said I would use an array.

If I do an array, which is what I was thinking of (still not 100% sure on how it would work) and I set the months like 1 => January, 2 => February and I am starting on April, being 4. When I get to 12 how do I loop around to 1 and continue till I hit 4?

 

edit: I could possibly do something like if my key is 12 to set it to 1 and start and kill it when my key hits the starting month. Shouldn't be too hard.

If you want to take the time to figure it out for an array, that is all the better learning experience and should produce a more efficient page.

 

However, if you do not need that bit of speed efficiency, the function about would work just fine. It is just not as "efficient" as it would be to make a function to utilize an array. But it sure is easier than coding that function/logic to utilize an array. That is for sure.

Nailed it first try:

$months_array = array(1=>'January', 2=>'February', 3=>'March', 4=>'April', 5=>'May', 6=>'June', 7=>'July', 8=>'August', 9=>'September', 10=>'October', 11=>'November', 12=>'December');

$output .= "<select name=\"due_date_month\">";
$i = date('n');
      $output .= "<option value=\"$i\">$months_array[$i]</option>";
if ($i == 12) { $i=1; } else { $i++; }

$m = date('n');
while ($i != $m){

      $output .= "<option value=\"$i\">$months_array[$i]</option>";
if ($i == 12) { $i=1; } else { $i++; }
}


$output .= "</select>";

 

Might be a way to clean it up some though?

$i = 0;
while($i     if($i == 0)
       echo date('F');
   else if(date('F', strtotime("+1 month")) != date('F'))
       echo date('F', strtotime("+1 month"));
   $i++;
}

Quick and Dirty way to just output it to text....one can easily create and array from this

 

 

NOTE:

I have not tested this...no guarentees...but the idea should work

$i = 0;
while($i < 12) {
    if($i == 0)
        echo date('F');
    else if(date('F', strtotime("+1 month")) != date('F'))
        echo date('F', strtotime("+1 month"));
    $i++;
}

Quick and Dirty way to just output it to text....one can easily create and array from this

 

Only problem is that doesn't start on current month or loop back after december to january. Though the post just above this shows I got it working correctly, was just seeing if it should/could be trimmed down any.

oh yeah...probably shouldn't have taken the $i's out of there

 

else if(date('F', strtotime("+{$i} month")) != date('F'))

echo date('F', strtotime("+{$i} month"));

 

 

No clue why it wouldn't start on the current month? What month does is start on then

 

EDIT: This works flawlessly for me....tested


$i = 0;
while($i     if($i == 0)
        echo "" . date('F') . "\n";
    else if(date('F', strtotime("+{$i} month")) != date('F'))
        echo "" . date('F', strtotime("+{$i} month")) . "\n";
    $i++;
}
?>

If you just want the current month to be preselected, another dirty way...

 

$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

foreach ($months as $key => $month) {
    $selected = (($key + 1) == date('n')) ? ' selected' : '';
    print '<option value="'.($key + 1).'"'.$selected.'>'.$month.'</option>';
}

 

Not tested but should work no probs!

or

<?php
$months_array = array(1=>'January', 2=>'February', 3=>'March', 4=>'April', 5=>'May', 6=>'June', 7=>'July', 8=>'August', 9=>'September', 10=>'October', 11=>'November', 12=>'December');
$output .= "<select name=\"due_date_month\">\n";
$m = date('n') - 1;
for ($j = 0; $j < 12; $j++) {
$i = ($m + $j) % 12 + 1;
    $output .= "\t<option value=\"$i\">$months_array[$i]</option>\n";
} echo
$output .= "</select>";
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.