Re: Dynamic Year Drop Down

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Nov 16, 2006, at 11:04 PM, Tom Ray [Lists] wrote:



Albert Padley wrote:
I want to build a select drop down that includes last year, the current year and 3 years into the future. Obviously, I could easily hard code this or use a combination of the date and mktime functions to populate the select. However, I'm looking for a more elegant way of doing this.

Thanks for pointing me in the right direction.

This works for me, simple and easy.

// Grabs current Year
$year=date("Y");

// Number of years you want total
$yearEnd=$year+10;

// Generate Drop Down
for($year=$year; $year <= $yearEnd; $year++) {
   print "<option value=\"$year\">$year</option>";
}

Thanks. This was close, but it didn't account for the first option being last year. I edited and ended up with this which works.

// Grabs current Year
$year=date("Y");
$yearFirst = $year-1;
// Number of years you want total
$yearEnd=$year+2;

// Generate Drop Down
echo "<select>\n";
for($year=$yearFirst; $year <= $yearEnd; $year++) {
   print "<option value=\"$year\">$year</option>";
}
echo "</select>\n";

Al Padley

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux