Re: Dynamic Year Drop Down

From: Date: Fri, 17 Nov 2006 07:08:02 +0000
Subject: Re: Dynamic Year Drop Down
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-244767@lists.php.net to get a copy of this message
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

« previous php.general (#244767) next »