Skylight_lady Posted August 28, 2011 Share Posted August 28, 2011 Hi Guys, In a form within PHP coding i can get the the next 10 years with the following code: <?php $date_future = date("Y", strtotime('+10 year')); $date_year = date("Y"); for($i=$date_year;$i<$date_future;$i++){ if($date_year == $i){ echo "<option value=\"$i\" selected=\"selected\">$i</option> \n"; } else { echo "<option value=\"$i\">$i</option> \n"; } } ?> I have tried to mess about with the same code and try and get it to work for the last 10 years without any luck. He's what i did: <?php $date_past = date("Y", strtotime('-10 year')); $date_year = date("Y"); for($i=$date_year;$i>$date_past;$i++){ if($date_year == $i){ echo "<option value=\"$i\" selected=\"selected\">$i</option> \n"; } else { echo "<option value=\"$i\">$i</option> \n"; } } ?> I would appreciate any help with this. Quote Link to comment https://forums.phpfreaks.com/topic/245845-get-last-10-years-in-form/ Share on other sites More sharing options...
darkfreaks Posted August 28, 2011 Share Posted August 28, 2011 how about this? $date_future= 10; for($i=$date_year;$i <= $date_future; ++$i){ //grabbing the next 10 years //echo output here ++$i; } Quote Link to comment https://forums.phpfreaks.com/topic/245845-get-last-10-years-in-form/#findComment-1262717 Share on other sites More sharing options...
darkfreaks Posted August 28, 2011 Share Posted August 28, 2011 actually it needs to be... $date_future= date("Y") -10; //subtract ten years for($i=$date_year;$i <= $date_future; ++$i){ //grabbing the next 10 years //echo output here ++$i; } Quote Link to comment https://forums.phpfreaks.com/topic/245845-get-last-10-years-in-form/#findComment-1262721 Share on other sites More sharing options...
Skylight_lady Posted August 28, 2011 Author Share Posted August 28, 2011 Hi, thanks for replying. I've been trying similar ways to what you showed but none work and neither dies your code as I get either a blank page or a empty year select box (when trying to get the previous 10 years, starting this year). Tho, it shouldn't be that complicated as my first code works for getting the future 10 years. Quote Link to comment https://forums.phpfreaks.com/topic/245845-get-last-10-years-in-form/#findComment-1262722 Share on other sites More sharing options...
darkfreaks Posted August 28, 2011 Share Posted August 28, 2011 can you post your code please Quote Link to comment https://forums.phpfreaks.com/topic/245845-get-last-10-years-in-form/#findComment-1262724 Share on other sites More sharing options...
darkfreaks Posted August 28, 2011 Share Posted August 28, 2011 http://www.phpcode.net/tag/strtotime Quote Link to comment https://forums.phpfreaks.com/topic/245845-get-last-10-years-in-form/#findComment-1262732 Share on other sites More sharing options...
Pikachu2000 Posted August 28, 2011 Share Posted August 28, 2011 A couple of ways to do it . . . // START WITH THIS // echo "<select name=\"year\">\n"; $now = date('Y'); $then = $now - 10; // THEN USE WITH THIS // $years = range( $now, $then ); foreach( $years as $v ) { echo "<option value=\"$v\">$v</option>\n"; } // OR THIS // for( $i = $now; $i >= $then; $i-- ) { echo "<option value=\"$i\">$i</option>\n"; } // THEN CLOSE THE FORM ELEMENT // echo "</select>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/245845-get-last-10-years-in-form/#findComment-1262736 Share on other sites More sharing options...
Psycho Posted August 28, 2011 Share Posted August 28, 2011 I prefer not to have hard-set values for the number of options to show. This should work as well and you can easily set the number fo years back to show by setting the first variable accordingly $years_to_show = 10; $start_year = date('Y'); for($offset=0; $offset<$years_to_show; $offset++) { $year = $start_year - $offset; echo "<option value=\"{$year}\">{$year}</option>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/245845-get-last-10-years-in-form/#findComment-1262742 Share on other sites More sharing options...
Skylight_lady Posted August 28, 2011 Author Share Posted August 28, 2011 Thanks guys, especially Pikachu2000. After studying his code i realised that the only reason my code would not work is because i used the code: $i++ in the line: for($i=$date_year;$i>$date_past;$i++){ When all i had to change it to was: $i-- Whats the meaning of that? I never had to use it before and always used $i++ or ++$i. It's best to know !! Quote Link to comment https://forums.phpfreaks.com/topic/245845-get-last-10-years-in-form/#findComment-1262871 Share on other sites More sharing options...
darkfreaks Posted August 28, 2011 Share Posted August 28, 2011 using $i++ or ++$i increments a variable by the number specified. any of the above examples show a way to properly loop 10 years. Quote Link to comment https://forums.phpfreaks.com/topic/245845-get-last-10-years-in-form/#findComment-1262876 Share on other sites More sharing options...
jcbones Posted August 28, 2011 Share Posted August 28, 2011 And $i-- or --$i de-increments a variable by the number specified. Quote Link to comment https://forums.phpfreaks.com/topic/245845-get-last-10-years-in-form/#findComment-1262878 Share on other sites More sharing options...
Psycho Posted August 28, 2011 Share Posted August 28, 2011 Plus, there is a significant difference between $i-- and --$i Per the manual (emphasis added): http://php.net/manual/en/language.operators.increment.php ++$a Pre-increment Increments $a by one, then returns $a. $a++ Post-increment Returns $a, then increments $a by one. --$a Pre-decrement Decrements $a by one, then returns $a. $a-- Post-decrement Returns $a, then decrements $a by one. Quote Link to comment https://forums.phpfreaks.com/topic/245845-get-last-10-years-in-form/#findComment-1262903 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.