Populating a dropdown with recent years
I have a <sel开发者_Python百科ect>
dropdown with which I want to populate the previous 20 years as well as the current year. How would I go about this?
It would be simpler to use strtotime()
echo strtotime("+1 year");
You can also use this inside a loop
echo strtotime("+".$i." year");
Where $i
is the index in the loop.
This should be all you need:
echo "<select>";
for ($i = date("Y") - 20; $i <= date("Y"); $i++)
{
echo "<option value='{$i}'>{$i}</option>";
}
echo "</select>";
What about trying something like this:
for (i=0;i<20;i++) {
echo mktime(0, 0, 0, date("m"), date("d"), date("Y")+i);
}
Seems like you just need a snippet like this:
$current_year = date("Y");
$min_year = 1990;
echo '<select name="year">';
echo '<option value="">Select Year..</option>";
for($i=$min_year; $i<=$current_year; $i++) {
echo '<option value="'.$i.'">'.$i.'</option>';
}
echo '</select>';
精彩评论