PHP Code For Year Month Day DropDown Select


Years Months and Days Drop-down List using PHP Code. using PHP creates a Dynamic year months days selecting option list with the current month, day, and year.

PHP Inline Code For The Year Selection Drop Down

Creating Year Selecting dropdown using PHP code. Copy and paste the code where you want the drop-down list to appear

PHP
<select id="year" name="year">
    <option value="">Select Year</option>
<?php
    $year_start  = 1940;
    $year_end = (int)date('Y'); // current Year
    $selected_year = 1992; // user date of birth year

    for ($i_year = $year_end; $i_year >= $year_start; $i_year--) {
      // Check if the current year should be selected
        $selected = $selected_year == $i_year ? ' selected' : '';
        echo '<option value="'.$i_year.'"'.$selected.'>'.$i_year.'</option>'."\n";
    }
?>
</select>

PHP Inline Code For The Day Selection Dropdown

This PHP code creates the day selection HTML dropdown. Generate the dropdown options for days 1 to 31

PHP
<select id="day" name="day">
    <option value="">Select Day</option>
<?php
    $selected_day = date('d'); // Get the current day of the month
    for ($i_day = 1; $i_day <= 31; $i_day++) {
      // Check if the current day is the selected day
        $selected = $selected_day == $i_day ? ' selected' : '';
        echo '<option value="'.$i_day.'"'.$selected.'>'.$i_day.'</option>'."\n";
    }
?>
</select>

PHP Inline Code For Month Selection Dropdown

This PHP code creates the month selection HTML dropdown

PHP
<select id="month" name="month">
    <option value="">Select Month</option>
<?php
    $selected_month = date('m'); // Get the current month as a number
    for ($i_month = 1; $i_month <= 12; $i_month++) {
       // Determine if the current month should be selected
        $selected = $selected_month == $i_month ? ' selected' : '';
        // Get the full name of the month
        $month_name = date('F', mktime(0, 0, 0, $i_month));
        echo '<option value="'.$i_month.'"'.$selected.'>('.$i_month.') '.$month_name.'</option>'."\n";
    }
?>
</select>

PHP Array of Month Names in Multiple Languages

Select the language from the list below. Then PHP array list names of months in your selected language will appear below.

Select the language to create a PHP array list of month names in Arabic, French, or German. And replace the month name variable below code

PHP array of months
// french month
$months_name = [
  "janvier", "février", "Mars", "avril", "Peut", "juin",
  "juillet", "août", "septembre", "octobre", "novembre", "décembre"
];
PHP
<select id="month" name="month">
  <option value="">Select Month</option>
  <?php
    $selected_month = date('m'); //current month
    // replace and add new months list
    // Arabic months
    $months_name = [
    "يناير", "فبراير", "مارس", "أبريل", "مايو", "يونيو",
    "يوليو", "أغسطس", "سبتمبر", "أكتوبر", "نوفمبر", "ديسمبر"
     ];

    for ($i_month = 1; $i_month <= 12; $i_month++) {
        $selected = ($selected_month == $i_month ? ' selected' : '');
        echo '<option value="'.$i_month.'"'.$selected.'>'. $months_name[$i_month-1].'</option>'."\n";
    }
?>
</select>

PHP code that creates a dropdown that selects the year of the Arabic numerals

PHP
<select id="year" name="year">
    <option value="">Select Year</option>
<?php
    function numberToArabic($number) {
        $number = (string)$number; // Convert the number to a string
        $arabic_num = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
        $newValue = '';
        foreach (str_split($number) as $digit) {
            // Handle decimal points or other non-numeric characters
            $newValue .= is_numeric($digit) ? $arabic_num[$digit] : $digit;
        }
        return $newValue;
    }

    $year_start  = 1940;
    $year_end = date('Y'); // current Year
    $user_selected_year = 1992;

    for ($i_year = $year_end; $i_year >= $year_start; $i_year--) {
        $selected = ($user_selected_year == $i_year ? ' selected' : '');
        $arabic_year = numberToArabic($i_year); // numbers to Arabic digits
        echo '<option value="'.$arabic_year.'"'.$selected.'>'.$arabic_year.'</option>'."\n";
    }
?>
</select>

PHP Functions to Create Day, Month, and Year Dropdowns

Below is the code to create a drop-down selection of year, month, and days using a PHP function code

PHP Function To Create Year Dropdown Selection

PHP
<?php
function yearDropdown($year_start=1940, $selected_year=0) {
    $year_end = date('Y');
    $year_start_length = strlen((string) $year_start);
    // Validate year_start
    if ($year_start > $year_end || $year_start_length !== 4) {
        return sprintf('Invalid start year: %s', htmlspecialchars($year_start, ENT_QUOTES, 'UTF-8'));
    }
    $option = '';
    $option .='<select id="year" name="year">'."\n";
    $option .= '<option value="0">year</option>'."\n";
    for ($i_year = $year_end; $i_year >= $year_start; $i_year--) {
        $selected = ($selected_year == $i_year ? ' selected' : '');
        $option .= '<option value="'.$i_year.'"'.$selected.'>'.$i_year.'</option>'."\n";
    }
    $option .= '</select>'."\n";
    return $option;
}

/* usage
 2 Parameters
 1 year start (1940)
 2 selected year (1992)
*/

echo yearDropdown(1940,1992);
?>

PHP Function To Create Month Dropdown Selection

PHP
<?php
function monthDropdown($selected_month=0) {
    $option = '';
    $option .= '<select id="month" name="month">'."\n";
    $option .= '<option value="0">Month</option>'."\n";
    for ($i_month = 1; $i_month <= 12; $i_month++) {
        $selected = ($selected_month == $i_month ? ' selected' : '');
        $month_name = date('F', mktime(0,0,0,$i_month));
        $option .= '<option value="'.$i_month.'"'.$selected.'>'.$month_name.'</option>'."\n";
    }
    $option .= '</select>'."\n";
    return $option;
}

/* usage
 1 Parameter
 selected month 4
*/

echo monthDropdown(4);
?>

PHP Function To Create Days Dropdown Selection

PHP
<?php
function dayDropdown($selected_day=0) {
    $option = '';
    $option .= '<select id="day" name="day">'."\n";
    $option .= '<option value="0">Day</option>'."\n";
    for ($i_day = 1; $i_day <= 31; $i_day++) {
        $selected = ($selected_day == $i_day ? ' selected' : '');
        $option .= '<option value="'.$i_day.'"'.$selected.'>'.$i_day.'</option>'."\n";
    }
    $option .= '</select>'."\n";
    return $option;
}

/* usage
 1 Parameter
 selected day 3
*/

echo dayDropdown(3);
?>

PHP Class Function Code for Generating Day, Month, and Year Dropdowns

Day, month, and year dropdown selection generating using the PHP class function code

Create a PHP file and add this code to it (date-dropdown-class.php). For inclusion on another page

PHP
<?php
/**
 * drop-down select year month day
 * https://www.html-code-generator.com/php/year-month-day-dropdown
 */
class DateDropDown {
    // year
    public function year($year_start=1940, $selected_year=0) {
        $year_end = date('Y'); // current year
        $year_length = strlen((string) $year_start);
        if ($year_start > $year_end || $year_start_length !== 4) {
            return sprintf('Invalid start year: %s', htmlspecialchars($year_start, ENT_QUOTES, 'UTF-8'));
        }
        $option = '';
        $option .='<select id="year" name="year">'."\n";
        $option .= '<option value="0">year</option>'."\n";
        for ($i_year = $year_end; $i_year >= $year_start; $i_year--) {
            $selected = ($selected_year == $i_year ? ' selected' : '');
            $option .= '<option value="'.$i_year.'"'.$selected.'>'.$i_year.'</option>'."\n";
        }
        $option .= '</select>'."\n\n";
        return $option;
    }
    // month
    public function month($selected_month=0) {
        $option = '';
        $option .= '<select id="month" name="month">'."\n";
        $option .= '<option value="0">Month</option>'."\n";
        for ($i_month = 1; $i_month <= 12; $i_month++) {
            $selected = ($selected_month == $i_month ? ' selected' : '');
            $option .= '<option value="'.$i_month.'"'.$selected.'>'. date('F', mktime(0,0,0,$i_month)).'</option>'."\n";
        }
        $option .= '</select>'."\n\n";
        return $option;
    }
    // day
    public function day($selected_day=0) {
        $option = '';
        $option .= '<select id="day" name="day">'."\n";
        $option .= '<option value="0">Day</option>'."\n";
        for ($i_day = 1; $i_day <= 31; $i_day++) {
            $selected = ($selected_day == $i_day ? ' selected' : '');
            $option .= '<option value="'.$i_day.'"'.$selected.'>'.$i_day.'</option>'."\n";
        }
        $option .= '</select>'."\n\n";
        return $option;
    }
}

?>

Usage class function:

PHP
<?php
include './date-dropdown-class.php';

// usage
$dd = new DateDropDown();

/*
  year 2 Parameters
  1 year start (1940)
  2 selected year (1992)
  year end current year
*/
echo $dd->year(1940, 1992);

/*
  month 1 Parameter
  selected month (4)
*/
echo $dd->month(4);

/*
  day 1 Parameter
  selected day (3)
*/
echo $dd->day(3);
?>