aebstract Posted April 6, 2009 Share Posted April 6, 2009 I'm trying to figure out a way to make a drop-down menu of all the months in the year. The only thing is it needs to start on the current month and go around and I am coming up blank with a method to do this. Any suggestions? Thanks Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 6, 2009 Share Posted April 6, 2009 would you prefer just having the current month as the default selected option in the dropdown box as apposed to altering the order in which the months are displayed? Quote Link to comment Share on other sites More sharing options...
aebstract Posted April 6, 2009 Author Share Posted April 6, 2009 Kind of needs to go in order, April May June etc Having current month as default is easy I'm sure this is also easy, I just can't think of how it would be done easily. I could possibly do an array and run it off of the keys or something. Quote Link to comment Share on other sites More sharing options...
premiso Posted April 6, 2009 Share Posted April 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
aebstract Posted April 6, 2009 Author Share Posted April 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
premiso Posted April 6, 2009 Share Posted April 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
aebstract Posted April 6, 2009 Author Share Posted April 6, 2009 I'm going with the array, I'll post it here to see if there is anything I may need to fix up a little bit whenever I finish it. Thanks Quote Link to comment Share on other sites More sharing options...
aebstract Posted April 6, 2009 Author Share Posted April 6, 2009 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? Quote Link to comment Share on other sites More sharing options...
Zane Posted April 6, 2009 Share Posted April 6, 2009 $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 Quote Link to comment Share on other sites More sharing options...
aebstract Posted April 6, 2009 Author Share Posted April 6, 2009 $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. Quote Link to comment Share on other sites More sharing options...
Zane Posted April 6, 2009 Share Posted April 6, 2009 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++; } ?> Quote Link to comment Share on other sites More sharing options...
Adam Posted April 6, 2009 Share Posted April 6, 2009 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! Quote Link to comment Share on other sites More sharing options...
sasa Posted April 6, 2009 Share Posted April 6, 2009 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>"; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.