Jump to content

Selecting current month in Month select dropdown


denhamd2

Recommended Posts

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;

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;
?>

<?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;
}?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.