Home>

About PHP's date of birth pulldown

// year
echo '<select name = "year">';
    for ($y = 1900;$y<date ('Y') + 1;$y ++) {
        echo "<option value = \" $y \ ">". $y. "</option>\ n";
    }
echo '</select>';
echo "year";
// Moon
$month = array ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", " 12 ");
echo '<select name = "month">';
foreach ($month as $key =>$value) {
    $month = $key + 1;
    echo "<option value = \" $month \ ">". $month. "</option>\ n";
}
echo '</select>';
echo "Month";
//Day
echo '<select name = "day">';
for ($d = 1;$d<= 31;$d ++) {
    echo "<option value = \" $d \ ">". $d. "</option>\ n";
}
echo '</select>';
echo "day";

I want to have an initial value here.

$year = "2000";
$month = "04";
$day = "04";


I knew until I used selected, but I don't know where to put it.
What should I do?

php
  • Answer # 1

    Only one hint.

    $year = "2000";
    for ($y = 1900;$y<date ('Y') + 1;$y ++) {
      $selected = "";
      if ($year == $y) {
         $selected = "selected";
      }
      echo "<option value = \" {$y} \ "{$selected}>{$y}</option>\ n";
    }

    Snacks:
    When putting a variable in a string, it is not good that it is not unified by connecting with. Or as it is, so I tried to unify it.

Related articles