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 code for the year select
Creating Year Selecting dropdown using PHP code. Copy and paste the code where you want the drop-down list to appear
<select id="year" name="year">
<option value="">Select Year</option>
<?php
$year_start = 1940;
$year_end = date('Y'); // current Year
$selected_year = 1992; // user date of birth year
for ($i_year = $year_start; $i_year <= $year_end; $i_year++) {
$selected = $selected_year == $i_year ? ' selected' : '';
echo '<option value="'.$i_year.'"'.$selected.'>'.$i_year.'</option>'."\n";
}
?>
</select>
PHP code for the day select
This PHP code creates the day selection HTML dropdown
<select id="day" name="day">
<option value="">Select Day</option>
<?php
$selected_day = date('d'); //current day
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";
}
?>
</select>
PHP code for the month select
This PHP code creates the month selection HTML dropdown
<select id="month" name="month">
<option value="">Select Month</option>
<?php
$selected_month = date('m'); //current month
for ($i_month = 1; $i_month <= 12; $i_month++) {
$selected = $selected_month == $i_month ? ' selected' : '';
echo '<option value="'.$i_month.'"'.$selected.'>('.$i_month.') '. date('F', mktime(0,0,0,$i_month)).'</option>'."\n";
}
?>
</select>
Names of months PHP array in different 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
// french month
$months_name = ["janvier", "février", "Mars", "avril", "Peut", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"];
<select id="month" name="month">
<option value="">Select Month</option>
<?php
$selected_month = date('m'); //current month
// replace and add new months list
$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>
Create a drop-down selection of Arabic number years using PHP
PHP code that creates a dropdown that selects the year of the Arabic numerals
<select id="year" name="year">
<option value="">Select Year</option>
<?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;
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";
}
?>
</select>
PHP functions for the day, month, year drop-down
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
function year_dropdow($year_start=1940, $selected_year=0) {
$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 year start (1940)
2 selected year (1992)
*/
echo year_dropdow(1940,1992);
?>
PHP function to create month dropdown selection
<?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);
?>
PHP function to create days dropdown selection
<?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 function code for the day, month, and year drop-down selection
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
/**
* drop-down select year month day
* https://www.html-code-generator.com/php/drop-down/year-month-day
*/
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_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\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
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);
?>