Приглашаем посетить
Путешествия (otpusk-info.ru)

Using Form Fields for Date Selection

Previous
Table of Contents
Next

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.

Selecting a Date Using an HTML Form (dateselection.php; excerpt)
<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>

Figure 3.4. The month names were automatically generated.

Using Form Fields for Date Selection


TIP

You can replace the microtimestamp() function with the following code:

function microtimestamp() {
  return microtime(true);
}

The function microtime() returns a float value of the current microtime, when the parameter true is provided (this was added in PHP 5). However, this is reportedly slower than using gettimeofday(), which just executes the underlying gettimeofday system call.



Previous
Table of Contents
Next