Page 1 of 1

Coding help for date selection form

Posted: Thu Jul 01, 2010 6:45 am
by pipedragon72
Help Please!

My level of php is very VERY limited but I need to find a solution to a problem with a form to select a date for booking a hotel room as it's showing dates like the 31st June and this in turn is causing a problem with the website's external booking system's server as it's not a valid date.

So, does anyone know what code is needed to NOT show all months up to the 31st; ie showing only Feb 28, June 30th etc etc?

The code for the form is:

Code: Select all

<?php
function selectDate (
                        $sel_d = 0      // selected day 
                      , $sel_m = 0       // selected month
                      , $sel_y = 0       // selected year
                      , $var_d = 'day'     // name for day variable
                      , $var_m = 'month'     // name for month variable
                      , $var_y = 'year'     // name for year variable
                      , $min_y = 0       // minimum year
                      , $max_y = 0       // maximum year
                      , $enabled = true  // enable drop-downs?
                    ) {
	echo '<form action="book-online.php" style="margin:0; padding:0;" method="post">
		  <p class="cgbook"><h3>Book online:</h3>Arriving on &nbsp;';
  //--------------------------------------------------------------------------
  // First of all, set up some sensible defaults
  // Default day is today - remove +1 if you wish to take bookings for today
  if ($sel_d == 0) 
    $sel_d = date('j')+1;
  // Default month is this month
  if ($sel_m == 0) 
    $sel_m = date('n');
  // Default year is this year
  if ($sel_y == 0) 
    $sel_y = date('Y');
  // Default minimum year is this year
  if ($min_y == 0) 
    $min_y = date('Y');
  // Default maximum year is one year ahead
  if ($max_y == 0) 
    $max_y = ($min_y + 1);
  // --------------------------------------------------------------------------
  // Start off with the drop-down for Days
  // Start opening the select element
  echo '<select name="'. $var_d. '" class="day"';
  // Add disabled attribute if necessary
  if (!$enabled) 
    echo ' disabled="disabled"';
  // Finish opening the select element
  echo '>\n';
  // Loop round and create an option element for each day (1 - 31)
  for ($i = 1; $i <= 31; $i++) {
    // Start the option element
    echo '\t<option value="'. $i. '"';
    // If this is the selected day, add the selected attribute
    if ($i == $sel_d) 
      echo ' selected="selected"';
    // Display the value and close the option element
    echo '>'. $i. '</option>';
  }
  // Close the select element
  echo '</select>';
  // --------------------------------------------------------------------------
  // Now do the drop-down for Months
  // Start opening the select element
  echo '<select name="'. $var_m. '" class="month"';
  // Add disabled attribute if necessary
  if (!$enabled) 
    echo ' disabled="disabled"';
  // Finish opening the select element
  echo '>\n';
  // Loop round and create an option element for each month (Jan - Dec)
  for ($i = 1; $i <= 12; $i++) {
    // Start the option element
    echo '\t<option value="'. $i. '"';
    // If this is the selected month, add the selected attribute
    if ($i == $sel_m) 
      echo ' selected="selected"';
    // Display the value and close the option element
    echo '>'. date('F', mktime(3, 0, 0, $i)). '</option>';
  }
  // Close the select element
  echo '</select>';
  // --------------------------------------------------------------------------
  // Finally, the drop-down for Years
  // Start opening the select element
  echo '<select name="'. $var_y. '" class="year"';
  // Add disabled attribute if necessary
  if (!$enabled) 
    echo ' disabled="disabled"';
  // Finish opening the select element
  echo '>\n';
  // Loop round and create an option element for each year ($min_y - $max_y)
  for ($i = $min_y; $i <= $max_y; $i++) {
    // Start the option element
    echo '\t<option value="'. $i. '"';
    // If this is the selected year, add the selected attribute
    if ($i == $sel_y) 
      echo ' selected="selected"';
    // Display the value and close the option element
    echo '>'. $i. '</option>';
  }
  // Close the select element
  echo '</select><br /> <br /> ';
  //Create Nights Dropdown - if you wish to display this in the html instead it being included in the selectDate function cut/paste this section into the form
  echo 'for	<select name="nights" id="nights" class="nights">
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
			<option value="4">4</option>
			<option value="5">5</option>
			<option value="6">6</option>
			<option value="7">7</option>
			<option value="8">8</option>
			<option value="9">9</option>
			<option value="10">10</option>
			<option value="11">11</option>
			<option value="12">12</option>
			<option value="13">13</option>
			<option value="14">14</option>
			<!-- add more nights here if required. no more than 28 -->
		</select> night(s)
 		';
	echo '
	   	 &nbsp;&nbsp; <input type="submit" class="submit" value="Check Availability" />
	</form></p><br /><br />';
} 
?>

Re: Coding help for date selection form

Posted: Thu Jul 01, 2010 7:37 am
by Benjamin
:arrow: Moved to PHP - Code

Re: Coding help for date selection form

Posted: Thu Jul 01, 2010 11:04 am
by Bill H
A couple of useful functions for building a selection form:

Code: Select all

function days_in_month($_month, $_year)
{
     if($_month == 2)
     {    return days_in_feb($_year);
     }
     elseif($_month == 4 || $_month == 6 || $_month == 9 || $_month == 11)
     {    return(30);
     }
     else return(31);
}

function days_in_feb($year)
{
     //$year must be YYYY [gregorian] leap year math :

     if ($year < 0) $year++;
     $year += 4800;

     if (($year % 4) == 0)
     {    if (($year % 100) == 0)
          {    if (($year % 400) == 0)
                    return(29);
               else return(28);
          }
          else return(29);
    }
    else return(28);
}