Skip to content

Instantly share code, notes, and snippets.

@pedroppinheiro
Created March 3, 2016 01:22
Show Gist options
  • Save pedroppinheiro/44a11029f1aab2e11efd to your computer and use it in GitHub Desktop.
Save pedroppinheiro/44a11029f1aab2e11efd to your computer and use it in GitHub Desktop.
code to display a select of year based on the current year
/** 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