PHP Year Month Day Drop Down 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 code for the year selection
Copy and paste the code where you want the drop-down list to appear
Year
<?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";
?>
PHP code for the day selection list
Day
PHP code for the month select list
Month
<?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
Select the month name in the local language and change the code below to replace the month name variable. ($months_name = [])
$months_name = ["janvier", "février", "Mars", "avril", "Peut", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"];
<?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 a drop-down list of Arabic number years using PHP
Year
<?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";
?>
Related:
PHP functions for the day, month, year drop-down
Year select
<?php
function year_dropdow($selected_year=0, $year_start=1940) {
$year_end = date('Y');
$year_length = strlen((string) $year_start);
if ($year_start > $year_end || $year_length < 4 || $year_length > 4) {
return 'invalid year start '.$year_start;
}
$option = '';
$option .='<select id="year" name="year">'."\n";
$option .= '<option value="0">year</option>'."\n";
for ($i_year = $year_start; $i_year <= $year_end; $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 selected year (1992)
// 2 year start (1940)
echo year_dropdow(1992,1940);
?>
Month select
<?php
function month_dropdow($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";
return $option;
}
// usage
// 1 Parameters
// selected month 4
echo month_dropdow(4);
?>
Day select
<?php
function day_dropdow($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 Parameters
// selected day 3
echo day_dropdow(3);
?>
PHP Class Object function code for the day, month, year drop-down
Create PHP file (date-dropdown.php) add this class function code
<?php
/**
* drop down year month day
* https://www.html-code-generator.com/php/drop-down/year-month-day
*/
class DateDropDown {
// year
public function year($selected_year=0, $year_start=1940) {
$year_end = date('Y'); // current year
$year_length = strlen((string) $year_start);
if ($year_start > $year_end || $year_length < 4 || $year_length > 4) {
return 'invalid year start '.$year_start;
}
$option = '';
$option .='<select id="year" name="year">'."\n";
$option .= '<option value="0">year</option>'."\n";
for ($i_year = $year_start; $i_year <= $year_end; $i_year++) {
$selected = ($selected_year == $i_year ? ' selected' : '');
$option .= '<option value="'.$i_year.'"'.$selected.'>'.$i_year.'</option>'."\n";
}
$option .= '</select>'."\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";
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";
return $option;
}
}
?>
usage class function
<?php
include './date-dropdown.php';
// usage
$dd = new DateDropDown();
// year = 2 Parameters
// 1 selected year (1992)
// 2 year start (1940)
echo $dd->year(1992, 1940);
// month = 1 Parameter. selected month (4)
echo $dd->month(4);
// day = 1 Parameter. selected day (3)
echo $dd->day(3);
?>