More Date Selection

Normally, we want a date picking tool to have a specific date preselected. Often the preselected date is the current one. Sometimes it needs to match a record in a database. Either way, we need a way of making the previous form a little more useful.

Here is an example of the HTML required to produce a dropdown box with an item preselected,

<select name='choose'>
<option value='whatever'>Whatever</option>
<option value='two' selected='SELECTED'>Two</option>
<option value='three'>Three</option>

To preselect an option, we add an attribute, selected='SELECTED'.

In order to know which items to select, our script would need to know the current date. The following PHP shows how to find and display the information we need.

$today = getdate();
echo "<p>Day ";
echo $today['mday'];
echo "<br>Month ";
echo $today['mon'];
echo "<br>Year ";
echo $today['year'];
echo "</p>";

We need to adapt the code we used to produce the original date selection form. The following shows how the code to display the month day can be shown.

echo "<p>Day: <select name='day'>";
for ($i=1;$i<=31;$i++)
      echo "<option value='".$i."' selected='SELECTED'>".$i."</option>\n";
      echo "<option value='".$i."'>".$i."</option>\n";
echo "</select>\n";

Once you have made sure that the other dropdown boxes have appropriate preselections, the next step would be to do something with the information that is submitted on the form.

One idea would be to use the PHP documentation to find out how to display the day of the week that corresponds to the date selected on the form.