john_6767 Posted January 24, 2007 Share Posted January 24, 2007 i'm trying to fill a drop down with the years from year 2000 to two years ahead of the current year, ie for this year there would be the values: 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 as options in the drop down and then next year 2010 would appear..i have been using the following code to display from two years back to one year ahead but cannot see how i can go from the year 2000 widthout having to update the code every year..maybe someone can show me how to get the current year minus the year 2000 then loop through adding every year between?[code]<select name="start_date" id="start_date" class="formDropDown"/> <option value="<?php echo date("Y") - 2 ?>"><?php echo date("Y") - 2 ?></option> <option value="<?php echo date("Y") - 1 ?>"><?php echo date("Y") - 1 ?></option> <option value="<?php echo date("Y")?>"><?php echo date("Y") ?></option> <option value="<?php echo date("Y") + 1 ?>"><?php echo date("Y") + 1 ?></option> </select>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/35463-fill-dropdown-with-the-years-from-2000-to-current/ Share on other sites More sharing options...
Jessica Posted January 24, 2007 Share Posted January 24, 2007 See this topic:http://www.phpfreaks.com/forums/index.php/topic,123562.0.html Quote Link to comment https://forums.phpfreaks.com/topic/35463-fill-dropdown-with-the-years-from-2000-to-current/#findComment-167799 Share on other sites More sharing options...
john_6767 Posted January 24, 2007 Author Share Posted January 24, 2007 cheers, i have just got this to work, its a bit messy, but incase it helps anyone else.. i'll now go check that link out and see if theres a better way to do it..[code] <?php $DateParam = date("Y"); $yearsBetween = $DateParam - 2000; $i=0; echo "<select name=\"start_date\" id=\"start_date\" class=\"formDropDown\"/>"; while($i<=$yearsBetween) { $yearVar = 2000 + $i; echo "<option value=\"" .$yearVar ."\">".$yearVar ."</option>"; $i++; } ?> <option value="<?php echo date("Y") + 1 ?>"><?php echo date("Y") + 1 ?></option> <option value="<?php echo date("Y") + 2 ?>"><?php echo date("Y") + 2 ?></option> </select>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/35463-fill-dropdown-with-the-years-from-2000-to-current/#findComment-167810 Share on other sites More sharing options...
kenrbnsn Posted January 24, 2007 Share Posted January 24, 2007 Did you read the thread referred to in the other reply? That shows you how to do this very simply:[code]<?php echo '<select name="start_date" id="start_date" class="formDropDown"/>'; $end_date = date('y') + 3; for($i=2000;$i<$end_date;$i++) echo '<option value="' .$i .'">'.$i .'</option>'; echo '</select>';?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/35463-fill-dropdown-with-the-years-from-2000-to-current/#findComment-167822 Share on other sites More sharing options...
john_6767 Posted January 24, 2007 Author Share Posted January 24, 2007 yep, just been reading it now, when i wrote my last post i already had what i'd done on the clipboard and didn't want to waste it! ;) cheers, that way is definitely better Quote Link to comment https://forums.phpfreaks.com/topic/35463-fill-dropdown-with-the-years-from-2000-to-current/#findComment-167824 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.