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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.