- Published on 6th Sep 2017
There are times when you need to insert date of birth <select> input box in your HTML form. Here's small PHP snippet to dynamically populate the HTML SELECT element with Date of Birth for you. Just copy and use in your projects.
//year
echo '<select name="year">';
for($i = date('Y'); $i >= date('Y', strtotime('-90 years')); $i--){
echo "<option value=\"$i\">$i</option>";
}
echo '</select>';
//month
echo '<select name="month">';
for($i = 1; $i <= 12; $i++){
$dt = DateTime::createFromFormat('!m', $i);
echo "<option value=\"$i\">".$dt->format('F')."</option>";
}
echo '</select>';
//date
echo '<select name="day">';
for($i = 1; $i <= 31; $i++){
echo "<option value=\"$i\">$i</option>";
}
echo '</select>';