用过去和未来的年份自动填充HTML选择框


Auto populate HTML Select box with years past and future

用PHP在表单中自动填充选择框的最佳方法是什么。

我希望能够在过去的15年里从现在开始,在未来的5年里从今年开始。

我在网上查了一下,只能找到它们在x年内的填充位置

<?php
$currentValue = 2008;
$past = 15;
$future = 5;
if ($currentValue && ($currentValue < date('Y') - $past || $currentValue > date('Y') + $future)) {
    echo "<option value='"$currentValue'" selected='"selected'">$currentValue</option>'n";
}
for ($year = date('Y') - $past; $year <= date('Y') + $future; $year++) {
    if ($year == $currentValue) {
        echo "<option value='"$year'" selected='"selected'">$year</option>'n";
    } else {
        echo "<option value='"$year'">$year</option>'n";
    }
}
相关文章: