mazman13 Posted January 13, 2009 Share Posted January 13, 2009 I'm trying to make a drop down box with years (2001, 2002, 2003, ect) but I don't want to enter then all and update them every year when the year changes. Whats the best way to do a drop down box using the current year and going back 20? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/140601-solved-making-a-drop-down-box-with-years/ Share on other sites More sharing options...
premiso Posted January 13, 2009 Share Posted January 13, 2009 <?php $currentYear = date("Y"); for ($year=$currentYear;$year>($currentYear-20);$year++) { echo $year . "<br />"; } ?> Should do it. Quote Link to comment https://forums.phpfreaks.com/topic/140601-solved-making-a-drop-down-box-with-years/#findComment-735762 Share on other sites More sharing options...
trq Posted January 13, 2009 Share Posted January 13, 2009 echo "<select name='year'>"; foreach (range(date('Y')-20, date('Y')) as $year) { echo " <option value='$year'>$year</option>"; } echo "</select>"; Quote Link to comment https://forums.phpfreaks.com/topic/140601-solved-making-a-drop-down-box-with-years/#findComment-735763 Share on other sites More sharing options...
timmah1 Posted January 13, 2009 Share Posted January 13, 2009 I have to throw in my two cents since this something I've been doing for awhile <?php $current = date("Y", strtotime('-20 years')); $future = date("Y", strtotime('+10 years')); $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December'); $weekday = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); $days = range (1, 31); $years = range ($current, $future); ?> Then, to call them in a drop-down <?php echo "Year: <select name='year'>"; foreach ($years as $value) { echo '<option value="'.$value.'">'.$value.'</option>\n'; } echo '</select><br />'; echo "Day: <select name='day'>"; foreach ($days as $value) { echo '<option value="'.$value.'">'.$value.'</option>\n'; } echo '</select><br />'; echo "Month: <select name='month'>"; foreach ($months as $value) { echo '<option value="'.$value.'">'.$value.'</option>\n'; } echo '</select><br />'; ?> [/code Quote Link to comment https://forums.phpfreaks.com/topic/140601-solved-making-a-drop-down-box-with-years/#findComment-735806 Share on other sites More sharing options...
dawsba Posted January 13, 2009 Share Posted January 13, 2009 Just for my 2 pence For getting months and days i hate typing the jan and feb over and over i use for($i=1;$i<=12;$i++){$month[$i]=date("F",mktime(0,0,0,$i+1,0,0));} gives $month[1]=january for($i=1;$i<=7;$i++){$days[$i]=date("l",mktime(0,0,0,0,$i+5,0));} gives $days[1] = monday hopefuilly some use to some1. Quote Link to comment https://forums.phpfreaks.com/topic/140601-solved-making-a-drop-down-box-with-years/#findComment-735862 Share on other sites More sharing options...
mazman13 Posted January 13, 2009 Author Share Posted January 13, 2009 Sounds great. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/140601-solved-making-a-drop-down-box-with-years/#findComment-736162 Share on other sites More sharing options...
mazman13 Posted January 13, 2009 Author Share Posted January 13, 2009 Ok with the foreach one, how to i make it so that the current year shows first? It prob needs to be in desc order. echo "<select name='year'>"; foreach (range(date('Y')-20, date('Y')) as $year) { echo " <option value='$year'>$year</option>"; } echo "</select>"; Quote Link to comment https://forums.phpfreaks.com/topic/140601-solved-making-a-drop-down-box-with-years/#findComment-736182 Share on other sites More sharing options...
kenrbnsn Posted January 13, 2009 Share Posted January 13, 2009 Just change the order in the range: <?php echo "<select name='year'>"; foreach (range(date('Y'), date('Y')-20) as $year) { echo " <option value='$year'>$year</option>"; } echo "</select>"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/140601-solved-making-a-drop-down-box-with-years/#findComment-736200 Share on other sites More sharing options...
mazman13 Posted January 13, 2009 Author Share Posted January 13, 2009 Great thanks! Quote Link to comment https://forums.phpfreaks.com/topic/140601-solved-making-a-drop-down-box-with-years/#findComment-736217 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.