searls03 Posted July 27, 2011 Share Posted July 27, 2011 so I want to make a drop down list with the current year all the way down to 1900. so lets say this year it lists 1900-2011, next year to 2012. I want it to do this automatically without anybody changing it. does anyone know how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/243015-birth-year/ Share on other sites More sharing options...
Pikachu2000 Posted July 27, 2011 Share Posted July 27, 2011 Here's a start: for( $i = 1900; $i <= date('Y', strtotime( '+ 1 year')); $i++ ) { Quote Link to comment https://forums.phpfreaks.com/topic/243015-birth-year/#findComment-1248178 Share on other sites More sharing options...
searls03 Posted July 27, 2011 Author Share Posted July 27, 2011 so how do I make a it a drop down list? Quote Link to comment https://forums.phpfreaks.com/topic/243015-birth-year/#findComment-1248185 Share on other sites More sharing options...
Pikachu2000 Posted July 27, 2011 Share Posted July 27, 2011 Echo the <select> tag, then echo each value in the for() loop into the <option></option> tags, then echo the </select> tag. Quote Link to comment https://forums.phpfreaks.com/topic/243015-birth-year/#findComment-1248186 Share on other sites More sharing options...
searls03 Posted July 27, 2011 Author Share Posted July 27, 2011 like? <?php for( $i = 1900; $i <= date('Y', strtotime( '- 1 year')); $i++ ) {echo '<select>'; echo '<option>'; echo $i; echo '</option>'; echo '</select>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/243015-birth-year/#findComment-1248188 Share on other sites More sharing options...
Pikachu2000 Posted July 27, 2011 Share Posted July 27, 2011 Did it work when you tried it? Quote Link to comment https://forums.phpfreaks.com/topic/243015-birth-year/#findComment-1248192 Share on other sites More sharing options...
searls03 Posted July 27, 2011 Author Share Posted July 27, 2011 nope came up with a different select box for each year. Quote Link to comment https://forums.phpfreaks.com/topic/243015-birth-year/#findComment-1248193 Share on other sites More sharing options...
xyph Posted July 27, 2011 Share Posted July 27, 2011 @Pikachu - This may be nitpicking, but PHP's date function is EXTREMELY slow. When you put it in a for loop, it will be executed at the start of each iteration, to verify the functions return value hasn't changed. Instead, use this $year = date('Y'); for( $i = 1900; $i <= $year; $i++ ) // ... Quote Link to comment https://forums.phpfreaks.com/topic/243015-birth-year/#findComment-1248226 Share on other sites More sharing options...
premiso Posted July 27, 2011 Share Posted July 27, 2011 nope came up with a different select box for each year. So re-arrange to figure out why it is not working...that is like all of coding, trial and error till you get it right or give up. <?php $dt = date('Y', strtotime( '- 1 year')); echo '<select name="birthyear">'; for( $i = 1900; $i <= $dt; $i++ ) { echo '<option value="'.$i.'">'; echo $i; echo '</option>'; } echo '</select>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/243015-birth-year/#findComment-1248230 Share on other sites More sharing options...
Pikachu2000 Posted July 28, 2011 Share Posted July 28, 2011 @Pikachu - This may be nitpicking, but PHP's date function is EXTREMELY slow. When you put it in a for loop, it will be executed at the start of each iteration, to verify the functions return value hasn't changed. Instead, use this $year = date('Y'); for( $i = 1900; $i <= $year; $i++ ) // ... No, you're absolutely right. I should have assigned the value to a variable first then used the variable. Quote Link to comment https://forums.phpfreaks.com/topic/243015-birth-year/#findComment-1248304 Share on other sites More sharing options...
Psycho Posted July 28, 2011 Share Posted July 28, 2011 You should put the years in reverse order. 90% of your users are going to have a birth year in the last 55 years rather than the first 55 years, and that is pretty much an industry standard in my opinion. Also, the code provided previously would exclude the current year. Here is my suggestion. $thisYear = date('Y'); $firstYear = 1900; echo '<select name="birthyear">'; for($year=$thisYear; $year>=$firstYear; $year++) { echo "<option value=\"{$year}\">{$year}</option>\n"; } echo '</select>'; Quote Link to comment https://forums.phpfreaks.com/topic/243015-birth-year/#findComment-1248308 Share on other sites More sharing options...
Pikachu2000 Posted July 28, 2011 Share Posted July 28, 2011 I'm sure you meant to decrement $year, rather than increment it . . . Quote Link to comment https://forums.phpfreaks.com/topic/243015-birth-year/#findComment-1248315 Share on other sites More sharing options...
Pikachu2000 Posted July 28, 2011 Share Posted July 28, 2011 It may even be easier to use range() with a foreach() loop and still end up with the same result. $years = range( date('Y'), 1900 ); Quote Link to comment https://forums.phpfreaks.com/topic/243015-birth-year/#findComment-1248318 Share on other sites More sharing options...
Psycho Posted July 28, 2011 Share Posted July 28, 2011 I'm sure you meant to decrement $year, rather than increment it . . . Yes I did. It may even be easier to use range() with a foreach() loop and still end up with the same result. $years = range( date('Y'), 1900 ); Interesting, I would have thought that using range() would choke on that since the larger number is given as the first parameter and the step value (the optional third parameter) defaults to "1". I was surprised that it was "smart" enough to use a -1 step value. Quote Link to comment https://forums.phpfreaks.com/topic/243015-birth-year/#findComment-1248333 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.