PHP Forums Archive

Select menu with current month

Tags: html, array, php scripts, date, select element

Olaf posted on 2009-09-16 20:04:18 #

This is a simple select form element with month notations. You can translate the names for local using. Based on the current date the actual month is selected (by default).


<?php
$curr_month = date("m");
$month = array (1=>"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$select = "<select name=\"month\">\n";
foreach ($month as $key => $val) {
    $select .= "\t<option val=\"".$key."\"";
    if ($key == $curr_month) {
        $select .= " selected=\"selected\">".$val."</option>\n";
    } else {
        $select .= ">".$val."</option>\n";
    }
}
$select .= "</select>";
echo $select;
?>

Comments / discussions

sakurasan posted on 2010-10-04 08:51:15 #

Hi Olaf!..

i have try ur code, and it worked. But can u give me some idea for my problem...
i spent many days googling about this, but can't help me...

i have 2 dropdown menu and 1 table (mysql) contain datetime field.
my problem is, i want to generate a dropdown menu that contain only month and year data extract from mysql datetime field.

when the data selected, ex. August, 2009 it will display other data from the database.

sorry for my poor english.

TQ

Olaf posted on 2010-10-05 06:14:22 #

Hi, check this PHP/JQuery example:

Populate select menus with jQuery and Ajax