Dynadot .com transfer sale, only $10.12 with coupon code DYNA12, available while supplies last, ends October 31, 2026.

Creating Dropdown Selector for Date Selection

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
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.
GoDaddyGoDaddy
CatchedCatched

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover

NamePros updates

New in Parking: updated way to act on many domains or inquiries at once
Your domain list and inquiry list now have a bar above and below the list. Check the items you want, then choose an action in the bar...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back