Dynadot โ€” .com Registration $8.99

Creating Dropdown Selector for Date Selection

Spaceship Spaceship
Watch

silentwind

Established Member
Impact
0
taken directly from my blog.
Sometimes, we need to create a form that need a date selection. Here's how I handle this thing. First, let us create some function to do this :
PHP:
<?php
/**
 * Form Dropdown Month
 *
 * Creates dropdown element for selecting month
 *
 * @access  public
 * @param   string  the name of the form element
 * @param   string  the selected value
 * @param   string  some extra parameter
 * @return  string
 */ 
function form_dropdown_month($name = '', $selected = '', $extra = '')
{
    if ($extra != '') $extra = ' '.$extra;

    $form = '<select name="'.$name.'"'.$extra.">\n";

    for($i = 1; $i <= 12; $i++){

        $start = mktime(0, 0, 0, $i, 1, date('Y'));
        $value = date('n', $start);
        $display = date('F', $start);
        $form .= '<option value="' . $value . '" ' . ($value === $selected ? 'selected' : '') . '>' . $display . "</option>\n";

    }

    $form .= "</select>\n";

    return $form;
}

/**
 * Form Dropdown Day
 *
 * Creates dropdown element for selecting day
 *
 * @access  public
 * @param   string  the name of the form element
 * @param   string  the selected value
 * @param   boolean leading zero value
 * @param   string  some extra parameter
 * @return  string
 */ 
function form_dropdown_day($name = '', $selected = '', $leadingZero = false, $extra = '')
{
    if ($extra != '') $extra = ' '.$extra;

    $form = '<select name="'.$name.'"'.$extra.">\n";

    for($i = 1; $i <= 31; $i++){

        $value = ($leadingZero ? (strlen($i) == 1 ? '0'.$i : $i) : $i);
        $display = ($leadingZero ? (strlen($i) == 1 ? '0'.$i : $i) : $i);
        $form .= '<option value="' . $value . '" ' . ($value === $selected ? 'selected' : '') . '>' . $display . "</option>\n";

    }

    $form .= "</select>\n";

    return $form;
}

/**
 * Form Dropdown Year
 *
 * Creates dropdown element for selecting year
 *
 * @access  public
 * @param   string  the name of the form element
 * @param   string  the selected value
 * @param   string  some extra parameter
 * @return  string
 */ 
function form_dropdown_year($name = '', $start, $end, $selected = '', $extra = '')
{
    if ($extra != '') $extra = ' '.$extra;

    $form = '<select name="'.$name.'"'.$extra.">\n";

    for($i = $end; $i >= $start; $i--){

        $form .= '<option value="' . $i . '" ' . ($i === $selected ? 'selected' : '') . '>' . $i . "</option>\n";

    }

    $form .= "</select>\n";

    return $form;
}
?>
Now, here's how we use these function :
PHP:
<?php echo form_dropdown_month('month_start', '1', 'id="month_start"'); ?> - 
<?php echo form_dropdown_day('day_start', '1', true, 'id="day_start"'); ?> - 
<?php echo form_dropdown_year('year_start', date('Y') - 50, date('Y'), '', 'id="year_start"'); ?></td>

That's it. Now, we have simple function to handle date selection on form.
Like it? rep please.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
Appraise.net
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back