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? Link to comment https://forums.phpfreaks.com/topic/45543-solved-months-dropdown/ 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. Link to comment https://forums.phpfreaks.com/topic/45543-solved-months-dropdown/#findComment-221091 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? Link to comment https://forums.phpfreaks.com/topic/45543-solved-months-dropdown/#findComment-221101 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); ?> Link to comment https://forums.phpfreaks.com/topic/45543-solved-months-dropdown/#findComment-221115 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? Link to comment https://forums.phpfreaks.com/topic/45543-solved-months-dropdown/#findComment-221266 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. Link to comment https://forums.phpfreaks.com/topic/45543-solved-months-dropdown/#findComment-221276 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 Link to comment https://forums.phpfreaks.com/topic/45543-solved-months-dropdown/#findComment-221279 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. Link to comment https://forums.phpfreaks.com/topic/45543-solved-months-dropdown/#findComment-221316 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.