Hi,

I am trying to convert my code for month and day drop down menus to be dynamic. What I want is, instead of having 31 days in every month, have them have the actual days they have, so if the user clicks Feb, 28 days will show and so on. Also, I'm trying to get leap year to work too, but with no luck... any help or direction pointing would be greatly appreciated.

Thanks,
Erik

<?php 


function make_calendar_pulldowns($m = NULL, $d = NULL, $y = NULL)	// This function makes three pull-down lists
									// for the months, days, and years.
    {

// Make the months array.
$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September',
		      'October', 'November', 'December');

// Make the months pull-down list
echo '<select name="month">';
foreach ($months as $key => $value)
	{
	    echo "<option value=\"$key\"";
	    if ($key == $m) { echo ' selected="selected"'; }
	    echo ">$value</option>\n";
	}
echo '</select>';

// Make the days pull-down list
echo '<select name="day">';
for ($day = 1; $day <= 31; $day++)
	{
	    echo "<option value=\"$day\"";
	    if ($day == $d) { echo ' selected="selected"'; }
	    echo ">$day</option>\n";
	}
echo '</select>';

// Make the years pull-down list
echo '<select name="year">';
for ($year = 2005; $year <= 2015; $year++)
	{
	    echo "<option value=\"$year\"";
	    if ($year == $y) { echo ' selected="selected"'; }
	    echo ">$year</option>\n";
	}
echo '</select>';

} 	// End of the function definition.

// Create the form tags.
echo '<h1 id="mainhead">Select a Date:</h1>
<p><br /></p><form action="dateform.php" method="post">';

// Call the function.
$dates = getdate();
make_calendar_pulldowns($dates['mon'], $dates['mday'], $dates['year']);

// End of form.
echo '</form><p><br /></p>';

// Print the current date and time
echo '<p>Today is ' . date('l') . '. The current time is ' . date('g:i a') . '.</p>';

?>

Here is my new arrays that I am using:

	// Make the months array.
     $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

 // Make the days array
 $days = array(1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

    You'll need to use JavaScript unless you reloading the page after each selection of date?

      This might work... I dont like writing plain vanilla JS (I use Dojo Toolkit (since I help write it)) but I believe this should work...

      <?php 
      function make_calendar_pulldowns($m = NULL, $d = NULL, $y = NULL)	// This function makes three pull-down lists
      									// for the months, days, and years.
          {
      
      // Make the months array.
      $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September',
      		      'October', 'November', 'December');
      
      // Make the months pull-down list
      echo '<select name="month" id="month" onchange="updateDays();">';
      foreach ($months as $key => $value)
      	{
      	    echo "<option value=\"$key\"";
      	    if ($key == $m) { echo ' selected="selected"'; }
      	    echo ">$value</option>\n";
      	}
      echo '</select>';
      
      // Make the days pull-down list
      echo '<select name="day">';
      for ($day = 1; $day <= 31; $day++)
      	{
      	    echo "<option value=\"$day\"";
      	    if ($day == $d) { echo ' selected="selected"'; }
      	    echo ">$day</option>\n";
      	}
      echo '</select>';
      
      // Make the years pull-down list
      echo '<select name="year" id="year" onchange="updateDays();">';
      for ($year = 2005; $year <= 2015; $year++)
      	{
      	    echo "<option value=\"$year\"";
      	    if ($year == $y) { echo ' selected="selected"'; }
      	    echo ">$year</option>\n";
      	}
      echo '</select>';
      
      } 	// End of the function definition.
      
      // Create the form tags.
      echo '<h1 id="mainhead">Select a Date:</h1>
      <p><br /></p><form action="dateform.php" method="post">';
      
      // Call the function.
      $dates = getdate();
      make_calendar_pulldowns($dates['mon'], $dates['mday'], $dates['year']);
      
      // End of form.
      echo '</form><p><br /></p>';
      
      // Print the current date and time
      echo '<p>Today is ' . date('l') . '. The current time is ' . date('g:i a') . '.</p>';
      
      ?>
      

      Javascript for Days menu (works with ALL years and months)

      function updateDays() {
      	var m = document.getElementById('month');
      	var y =  document.getElementById('year');
      	var d = document.getElement.byId('day');
      	var check = new Date(Number(y.value), Number(m.value),1); //new Date object with 1st of month
      	check.setMonth(check.getMonth()+1); //roll to next month
      	check.setDate(check.getDate()-1); //roll back 1 day, this gets the last day of the original month regardless of number of days or leap year
      	if(d.options[d.length-1].value > check.getDate()) {
      		while(d.options[d.length-1].value > check.getDate()){
      			d.options[d.length-1] = null;
      		}
      	}elseif(d.options[d.length-1].value < check.getDate()) {
      		while(d.options[d.length-1].value > check.getDate()){
      			d.options[d.length] = new Option(d.options[d.length-1].value+1,d.options[d.length-1].value+1);
      		}
      	}
      }
      
        Write a Reply...