sotusotusotu Posted September 26, 2007 Share Posted September 26, 2007 Hello, I am trying to create a drop down list with 12 months as the contents. I need to be able to determine what the latest month is and then put the 11 months before into it (in the correct order). For example, it is September 2007 obviously at the moment, so that will need to be the last and latest month in the list. The other 11 will be in reverse order going back to August 2006. I am a newbie at PHP so could give me any ideas please? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/70777-solved-drop-down-list-of-months/ Share on other sites More sharing options...
GingerRobot Posted September 26, 2007 Share Posted September 26, 2007 How about: <?php echo '<select name="month">'; for($x=0;$x<12;$x++){ $time = strtotime("$x months ago"); $month = date('F Y',$time); echo '<option value="'.$x.'">'.$month.'</month>'; } echo '</select>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/70777-solved-drop-down-list-of-months/#findComment-355866 Share on other sites More sharing options...
sotusotusotu Posted September 26, 2007 Author Share Posted September 26, 2007 Thanks a lot mate. That displays exactly how I want it to. Just one more thing I need the list to link to pages. For example, September 2007 needs to link to "?month=sep2007" all the way down to "?month=oct2007". Can this be achieved with that code? Quote Link to comment https://forums.phpfreaks.com/topic/70777-solved-drop-down-list-of-months/#findComment-355878 Share on other sites More sharing options...
GingerRobot Posted September 26, 2007 Share Posted September 26, 2007 Well, if you changed the value of the option to: <?php echo '<select name="month">'; for($x=0;$x<12;$x++){ $time = strtotime("$x months ago"); $month = date('F Y',$time); $link = date('MY',$time); echo '<option value="'.$link.'">'.$month.'</month>'; } echo '</select>'; ?> And submitted the form with GET then yes, it would work. Quote Link to comment https://forums.phpfreaks.com/topic/70777-solved-drop-down-list-of-months/#findComment-355884 Share on other sites More sharing options...
cooldude832 Posted September 26, 2007 Share Posted September 26, 2007 Or since get doesn't need to validate with refer just make it hyper links if that is your goal. echo "<a href=\"page.php?month=sept07\">September 07</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/70777-solved-drop-down-list-of-months/#findComment-355902 Share on other sites More sharing options...
sotusotusotu Posted September 26, 2007 Author Share Posted September 26, 2007 Thanks guys. I tried it in a GET form but the month doesn't seem to change in the url. The month is always sept2007. <form method="get" action=""> <p> <?php echo '<select name="month">'; for($x=0;$x<12;$x++){ $time = strtotime("$x months ago"); $month = date('F Y',$time); $link = date('MY'); echo '<option value="'.$link.'">'.$month.'</month>'; } echo '</select>'; ?> </p> <p> <label> <input type="submit" value="Submit" /> </label> </p> </form> Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/70777-solved-drop-down-list-of-months/#findComment-355918 Share on other sites More sharing options...
GingerRobot Posted September 26, 2007 Share Posted September 26, 2007 You made a little mistake when copying my post. Change this line: $link = date('MY'); to: $link = date('MY',$time); Without the second parameter, the date function will return the formatted string for the current date, hence the reason why the value was always sep2007 (well, it would be till we get to october anyway!) Quote Link to comment https://forums.phpfreaks.com/topic/70777-solved-drop-down-list-of-months/#findComment-355919 Share on other sites More sharing options...
sotusotusotu Posted September 26, 2007 Author Share Posted September 26, 2007 Stupid me . Thanks a lot for your help! Quote Link to comment https://forums.phpfreaks.com/topic/70777-solved-drop-down-list-of-months/#findComment-355926 Share on other sites More sharing options...
sotusotusotu Posted September 26, 2007 Author Share Posted September 26, 2007 Sorry one more thing if I can be a pain. Can I just get the link to show the abbreviated month in lower case without the date after it? Quote Link to comment https://forums.phpfreaks.com/topic/70777-solved-drop-down-list-of-months/#findComment-355930 Share on other sites More sharing options...
GingerRobot Posted September 26, 2007 Share Posted September 26, 2007 Try using: $link = strtolower(date('M',$time)); You'll find all the formatting characters you need here: www.php.net/date I also used to the strtolower function to make all the characters lower case. Quote Link to comment https://forums.phpfreaks.com/topic/70777-solved-drop-down-list-of-months/#findComment-355935 Share on other sites More sharing options...
sotusotusotu Posted September 26, 2007 Author Share Posted September 26, 2007 Thanks again. Seems like a useful site. I do need all the help I can get. Quote Link to comment https://forums.phpfreaks.com/topic/70777-solved-drop-down-list-of-months/#findComment-355936 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.