denhamd2 Posted April 4, 2007 Share Posted April 4, 2007 Hi, I'm looking to do a dropdown with the months in the year starting with this month and going on for 12 months, ie. the selected month is April 2007 and the last month is March 2008, does anyone know how to do this using PHP? Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 4, 2007 Share Posted April 4, 2007 Try this: <?php $out = "<select name=\"month\">\n"; $out .= "<option value=\"\"></option>\n"; for ($i = 0; $i < 12; $i++) { $ts = mktime(0,0,0,(date('m') + $i),1); $month = date('m', $ts); $year = date('Y', $ts); $out .= "<option value=\"$ts\">$month $year</option>\n"; } $out .= "</select>\n"; ?> Untested, but the principle should be solid. Quote Link to comment Share on other sites More sharing options...
denhamd2 Posted April 4, 2007 Author Share Posted April 4, 2007 great thanks! just wondering aswell, I have the month and year stored in a variable $thedate so for example $thedate=042007 is there any way of splitting this up into 2 separate variables, $themonth and $theyear? Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 4, 2007 Share Posted April 4, 2007 You could use substr() to extract them: <?php $thedate = '042007'; $month = substr($thedate, 0, 2); $year = substr($thedate, -4); ?> Quote Link to comment Share on other sites More sharing options...
denhamd2 Posted April 4, 2007 Author Share Posted April 4, 2007 brilliant thanks! going with obsidian's code is there any way to have the current month selected? Quote Link to comment Share on other sites More sharing options...
aebstract Posted April 4, 2007 Share Posted April 4, 2007 You could just run an if statement in your for statement, if the current matches up with the one that it is on, then set it, then run your else as the normal. Quote Link to comment Share on other sites More sharing options...
denhamd2 Posted April 4, 2007 Author Share Posted April 4, 2007 ok great thanks seemed to have worked it out Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 4, 2007 Share Posted April 4, 2007 Since you're always building your list from the current month on, just leave off the empty option, and the current month will always be selected by default. Quote Link to comment 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.