Created
March 3, 2016 01:22
-
-
Save pedroppinheiro/44a11029f1aab2e11efd to your computer and use it in GitHub Desktop.
code to display a select of year based on the current year
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** source: http://stackoverflow.com/a/18608587/1252947 **/ | |
<?php | |
<select id="year"> | |
<? | |
$currentValue = date("Y"); | |
$past = 20; | |
$future = 0; | |
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"; | |
} | |
} | |
?> | |
</select> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment