PHPRO.ORG

Here is another useful dropdown menu list for forms. This will create a dropdown select of days of the weeks. It should be noted that the week begins on Monday which is day one, as defined by ISO-8601.

The PHP date function date('N') will return the ISO-8601 weekday number. This differs from the date('l') which returns the weekday, beginning with zero for Sunday. In ISO-8601, Sunday is number 7.


<?php

/*
 *
 * @Create an HTML drop down menu
 *
 * @param string $name The element name and ID
 *
 * @param int $selected The day to be selected
 *
 * @return string
 *
 */
function dayDropdown($name="day"$selected=null)
{
        
$wd '<select name="'.$name.'" id="'.$name.'">';

        
$days = array(
                
=> 'Monday',
                
=> 'Tuesday',
                
=> 'Wednesday',
                
=> 'Thursday',
                
=> 'Friday',
                
=> 'Saturday',
                
=> 'Sunday');
        
/*** the current day ***/
        
$selected is_null($selected) ? date('N'time()) : $selected;

        for (
$i 1$i <= 7$i++)
        {
                
$wd .= '<option value="'.$i.'"';
                if (
$i == $selected)
                {
                        
$wd .= ' selected';
                }
                
/*** get the day ***/
                
$wd .= '>'.$days[$i].'</option>';
        }
        
$wd .= '</select>';
        return 
$wd;
}


/*** example usage ***/
$name 'my_dropdown';
$day 3;

echo 
dayDropdown($name$day);

?>

Demonstration