Simple Date Selection

Date pickers are fairly standard user interface elements these days. There are many DHTML implementations of this. This script is all about how PHP control structures can be used to make a form with dropdown boxes.

<title>Working With Dates</title>
<h1>Choosing A Date On A Form</h1>
<form action="date.php" method="POST">
// Choosing a day 1-31
echo "<p>Day: <select name='day'>";
for ($i=1;$i<=31;$i++)
   echo "<option value='".$i."'>".$i."</option>\n";
echo "</select>\n";
// Choosing a month Jan-Dec
$months= array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October ', 'November', 'December');
echo " Month: <select name='month'>";
for ($i=1;$i<=12;$i++)
   echo "<option value='".$i."'>".$months[$i-1]."</option>\n";
echo "</select>\n";
// Choosing a year
echo " Year: <select name='year'>";
for ($i=2010;$i<=2020;$i++)
   echo "<option value='".$i."'>".$i."</option>\n";
echo "</select></p>\n";
echo "<p><input type='reset' value='Clear'> <input type='submit' value='Select Date'></p>\n";

There are a few things to learn from this example. Once you have the page working correctly, look at the HTML that was sent to the browser. (View Source)

In producing scripts like this, remember to consider the goal. Almost all of our programming is to ensure that a correct page of HTML is produced.

Notice how the months were done. The value of each option is the number of the month. We do this because that is the best way to work with the information when the form is submitted. The user, however, needs to see a list of words for the months.