選択フォーム4|開始年は今年から-100年, 既定値がある場合はselectedを追加

Demo

PHP&CSS

PHPindex.phpdownload

<div class="php_bir_select4">

<?php

	// 使用している変数名
	// $selected_year, $selected_month, $selected_day, $d, $this_year, $start_year
	// $birth_year, $birth_month, $birth_day, $i, $y_selected, $m_selected, $d_selected

	/* 既定値を設定 */
	$selected_year = "2020";//年

	$selected_month = "4";//月

	$selected_day = "10";//日

	/*----------------------------------------*/

	$d = date_create();

	$this_year = date_format($d, "Y");

	$start_year = $this_year - 100;

	$birth_year = $birth_month = $birth_day = "<option value=\"\">--</option>";

	for($i = $start_year; $i <= $this_year; $i++){

		$y_selected = "";

		if($i == $selected_year){

			$y_selected = " selected";
		}

		$birth_year .= "<option value=\"{$i}\"{$y_selected}>{$i}</option>";
	}

	for($i = 1; $i <= 12; $i++){

		$i = sprintf('%02d', $i);

		$m_selected = "";

		if($i == $selected_month){

			$m_selected = " selected";
		}

		$birth_month .= "<option value=\"{$i}\"{$m_selected}>{$i}</option>";
	}

	for($i = 1; $i <= 31; $i++){

		$i = sprintf('%02d', $i);

		$d_selected = "";

		if($i == $selected_day){

			$d_selected = " selected";
		}

		$birth_day .= "<option value=\"{$i}\"{$d_selected}>{$i}</option>";
	}

	echo "<select name=\"birth_year\">{$birth_year}</select>年<select name=\"birth_month\">{$birth_month}</select>月<select name=\"birth_day\">{$birth_day}</select>日";
?>
</div>

CSSstyle.cssdownload

.php_bir_select4 select{
	width: 4rem;
	outline: none;
	padding: 0.3rem 0;
	margin: 0 0.2rem 0 0.5rem;
	border: 1px solid #ccc;
}
.php_bir_select4 select:first-child{
	margin-left: 0rem;
}
© 2025 wayday