Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Generate a dropdown with months in a year (See related posts)

The below code will generate a form dropdown for all 12 months, with the current month selected. Handy for those date pickers!

<select name="start_month">
<?php
global $LOC;
$current_time_m = $LOC->decode_date('%m', $LOC->now);

for ($i = 1; $i <= 12; $i++) {
   echo "<option value='$i-'";
if ($i == $current_time_m) { echo " selected='selected'"; }
$month_text = date("F", mktime(0, 0, 0, $i+1, 0, 0, 0));
   echo ">$month_text</option>
"; } ?>
</select>

Comments on this post

davedavis posts on Sep 27, 2006 at 11:52
This might be useful for day or year ranges, but how is this good for months? How many months are there this year? 12. How many months are there next year? 12. How many were there 10 years ago? 12.

I'm seeing a pattern, here. It may as well just be a static array, or set of constants, which you foreach through.

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts