Home > Articles

Date and Time

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Using Form Fields for Date Selection

<select name="month"><?php
 for ($i = 1; $i <= 12; $i++) {
  $monthname = date(F, mktime(12, 0, 0, $i, 1,     2005));
  echo "<option value=\"$i\">$monthname</option>";
  }
?></select>

If you want to offer an HTML form to select a date, like many hotel- and flight-booking services offer, you can use the various parameters of date(); loop through all months of the year; and, therefore, create a selection list of days, months, and years. The preceding code contains the code for this; see Figure 3.4 for the result.

<form method="post" action="<?php echo 
  htmlspecialchars($_SERVER[’PHP_SELF’]); ?>">
 <select name="day"><?php
  for ($i = 1; $i <= 31; $i++) {
   echo "<option value=\"$i\">$i</option>\n";
  }
 ?></select>
 <select name="month"><?php
  for ($i = 1; $i <= 12; $i++) {
   $monthname = date(‘F‘, mktime(12, 0, 0, $i, 1, 
     2005));
   echo "<option value=\"$i\">$monthname</     option>";
  }
 ?></select>
 <select name="year"><?php
  for ($i = 2005; $i <= 2010; $i++) {
   echo "<option value=\"$i\">$i</option>";
  }
 ?></select>
</form>

Selecting a Date Using an HTML Form (dateselection.php; excerpt)

  • + Share This
  • 🔖 Save To Your Account