Jump to content

Help with date selection form code


pipedragon72

Recommended Posts

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:

 

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

//--------------------------------------------------------------------------

// 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>

 

';

//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 '

   <input type="submit" class="submit" value="Check Availability" />

</form></p>

 

';

}

?>

Link to comment
https://forums.phpfreaks.com/topic/206386-help-with-date-selection-form-code/
Share on other sites

You've a snippet of code that calls this function?

 

EDIT:

NVM

 

Do something like this here...

// Loop round and create an option element for each day (1 - 31)
for ($i = 1; $i <= 31; $i++) {

 

change to this granted if the months are passed with that style of name to the function as in June or April... etc..

 

// Loop round and create an option element for each day (1 - 31)
$max = 31;
if($var_m == "June" || $$var_m == "April") {$max = 30;}
if($var_m == "February") {
// Will need to check for leap year also... but for simplistic idea
$max = 28;}

for ($i = 1; $i <= $max; $i++) {

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.