Dropdown List Using PHP

Years Months and Days Dropdown List With PHP Code

Years Dropdown List Using PHP

PHP
<?php 
    $year_start  = 1940;
    $year_end = date('Y'); // current Year
    $user_selected_year = 1992; // user date of birth year

    echo '<select id="year" name="year">'."\n";
    for ($i_year = $year_start; $i_year <= $year_end; $i_year++) {
        $selected = ($user_selected_year == $i_year ? ' selected' : '');
        echo '<option value="'.$i_year.'"'.$selected.'>'.$i_year.'</option>'."\n";
    }
    echo '</select>'."\n";
?>

Days Dropdown List Using PHP

PHP
<?php
    $selected_day = date('d'); //current day

    echo '<select id="day" name="day">'."\n";
    for ($i_day = 1; $i_day <= 31; $i_day++) { 
        $selected = ($selected_day == $i_day ? ' selected' : '');
        echo '<option value="'.$i_day.'"'.$selected.'>'.$i_day.'</option>'."\n";
    }
    echo '</select>'."\n";
?>

Months Dropdown List Using PHP

PHP
<?php
    $selected_month = date('m'); //current month

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

Name of months in local languages

PHP
$months_name = ["janvier", "février", "Mars", "avril", "Peut", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"];
PHP
<?php
    $selected_month = date('m'); //current month
    // replace and add new months list
    $months_name = ["janvier", "février", "Mars", "avril", "Peut", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"];

    echo '<select id="month" name="month">'."\n";
    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";
    }
    echo '</select>'."\n";
?>

Create Arabic Number Years Dropdown List Using PHP

PHP
<?php 
    function numberToArabic($number){ 
        $number = (string)$number;
        $arabic_num = ['٠','١','٢','٣','٤','٥','٦','٧','٨','٩'];
        $newValue = "";
        for ($i = 0; $i < strlen($number); $i++) {
            $newValue .= $arabic_num[$number[$i]];
        }
        return $newValue;
    }

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

    echo '<select id="year" name="year">'."\n";
    for ($i_year = $year_start; $i_year <= $year_end; $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";
    }
    echo '</select>'."\n";
?>