这是我显示选择框的示例代码.但是选项列表中总是缺少2月份.
不熟悉PHP,也会建议我如何改进这段代码.
$birth_month = strtotime("-{$age} Years");
$birthday = strftime("%m.%Y", $birthday_timestamp);
for ($month_itr = 1; $month_itr <= $bis; month_itr++) {
$option_value = strftime("%m.%Y", $birth_month );
$option_text = strftime("%b. %y", $birth_month );
$selected = ($birthday == $option_value ? 'selected="selected"' : '');
echo "<option value='{$option_value}' {$selected}>{$option_text}</option>\n";
$birth_month = strtotime("+1 Month", $birth_month);
}
Run Code Online (Sandbox Code Playgroud)
$birth_month = strtotime("-{$age} Years");
Run Code Online (Sandbox Code Playgroud)
此行正在计算相对于今天的日期.今天是30日,日期将是相关月份的30日.鉴于您使用"+1月"滚动浏览这几个月,您将始终关注每个月的30日.由于2月30日不存在,PHP会将此计算为3月2日(即2月28日之后的2天).如果你使用类似的东西
$birth_month = strtotime("-{$age} Years");
$birth_month = mktime( 0, 0, 0, date( 'm', $birth_month ), 1, date( 'Y', $birth_month ) );
Run Code Online (Sandbox Code Playgroud)
你应该没事.