denhamd2 Posted July 26, 2007 Share Posted July 26, 2007 Hi, I have set up a simple dropdown to list all the months a year from now and for the past year, so form July 2006 - Jun 2008. However, I am looking to have the current month as the selected option when the user first sees the page. Any ideas on how to do this? Here is my code currently: // Dropdown for months $out = "<select name=\"thedate\">\n"; for ($i = 0; $i < 12; $i++) { $ts = mktime(0,0,0,(date('m') + $i),1); $month = date('m', $ts); $monthname = date('F', $ts); $year = date('Y', $ts); $year = ($year-1); $out .= "<option value=\"$month$year\">$monthname $year</option>\n"; } for ($i = 0; $i < 12; $i++) { $ts = mktime(0,0,0,(date('m') + $i),1); $month = date('m', $ts); $monthname = date('F', $ts); $year = date('Y', $ts); $out .= "<option value=\"$month$year\">$monthname $year</option>\n"; } $out .= "</select>\n"; echo $out; Quote Link to comment Share on other sites More sharing options...
DeadEvil Posted July 26, 2007 Share Posted July 26, 2007 try this code.. \m/ <? $current = date('mY'); // Dropdown for months $out = "<select name=\"thedate\">\n"; $isSelected = ''; for ($i = 0; $i < 12; $i++) { $ts = mktime(0,0,0,(date('m') + $i),1); $month = date('m', $ts); $monthname = date('F', $ts); $year = date('Y', $ts); $year = ($year-1); $my = $month.$year; $isSelected = $current == $my ? 'selected="selected"' : ''; $out .= "<option value=\"$my\" $isSelected>$monthname $year</option>\n"; } $isSelected = ''; for ($i = 0; $i < 12; $i++) { $ts = mktime(0,0,0,(date('m') + $i),1); $month = date('m', $ts); $monthname = date('F', $ts); $year = date('Y', $ts); $my = $month.$year; $isSelected = $current == $my ? 'selected="selected"' : ''; $out .= "<option value=\"$my\" $isSelected>$monthname $year</option>\n"; } $out .= "</select>\n"; echo $out; ?> Quote Link to comment Share on other sites More sharing options...
vbnullchar Posted July 26, 2007 Share Posted July 26, 2007 <?php function date_select($mon_id="month", $day_id="day",$year_id="year",$selmon=0,$selday=0,$selyear=0) { $m= "<select id=$mon_id>"; for($i=1; $i<=12; $i++) { $m.= "<option value=$i ". ($selmon==$i?'selected':' ') .">".date('M', mktime(0,0,0,$i,date('d'),date('Y')))."</option>"; } $m.= "</select> "; $m.= "<select id=$day_id>"; for($i=1; $i<=31; $i++) { $m.= "<option value=$i ".($selday==$i?'selected':' ').">$i</option>"; } $m.= "</select> "; $m.= "<select id=$year_id>"; for($i=2007; $i<=2020; $i++) { $m.= "<option value=$i ".($selyear==$i?'selected':' ').">$i</option>"; } $m.= "</select> "; return $m; }?> 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.