Create month and year dropdown list using PHP

by  
Share:
month year day

There are times when you need to create month and year dropdown list dynamically. I have used this code in many of my project’s reporting section. So I am sharing this useful piece of code with all of you.

Create year dropdown list

Creating the year dropdown list is extremely easy. Just give a start and end year range and your year array is created, now just loop it over.

<?php
// set start and end year range
$yearArray = range(2000, 2050);
?>
<!-- displaying the dropdown list -->
<select name="year">
    <option value="">Select Year</option>
    <?php
    foreach ($yearArray as $year) {
        // if you want to select a particular year
        $selected = ($year == 2015) ? 'selected' : '';
        echo '<option '.$selected.' value="'.$year.'">'.$year.'</option>';
    }
    ?>
</select>

Create month dropdown list

There are two ways you can create the month dropdown list. First one is where you define a php array variable with all the month list and just loop over and display the dropdown. Have a look at the first method below.

<?php
// set the month array
$formattedMonthArray = 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",
                );
?>
<!-- displaying the dropdown list -->
<select name="month">
    <option value="">Select Month</option>
    <?php
    foreach ($monthArray as $month) {
        // if you want to select a particular month
        $selected = ($month == 5) ? 'selected' : '';
        // if you want to add extra 0 before the month uncomment the line below
        //$month = str_pad($month, 2, "0", STR_PAD_LEFT);
        echo '<option '.$selected.' value="'.$month.'">'.$formattedMonthArray[$month].'</option>';
    }
    ?>
</select>

The other method is a bit tricky as it relies on php function rather rather than defining static month array.
<?php
$monthArray = range(1, 12);
?>
<select name="month">
    <option value="">Select Month</option>
    <?php
    foreach ($monthArray as $month) {
        // padding the month with extra zero
        $monthPadding = str_pad($month, 2, "0", STR_PAD_LEFT);
        // you can use whatever year you want
        // you can use 'M' or 'F' as per your month formatting preference
        $fdate = date("F", strtotime("2015-$monthPadding-01"));
        echo '<option value="'.$monthPadding.'">'.$fdate.'</option>';
    }
    ?>
</select>

If you have any other tricks or suggestion to optimize this code please share with us.

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.

Share:
Hasan

Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

pandiyan on

how do generate in particular month and year
e.g. 2017 and march month only export the excel file..

Reply
Shahrukh Khan on

you want xls file export.

Reply
Asyira on

Why i can’t display a name of month in dropdown menu? Can you help me with that? Thank you.

Reply
Shahrukh Khan on

what kind of error are you getting.

Reply
vicky on

can you please provide backend code using laravel

Reply
Shahrukh Khan on

no thats not possible. sorry 🙁

Reply
Elango on

Great help! Thank you for your good sharing!

Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.