␡
- Using Text Within date()
- Automatically Localizing Dates
- Manually Localizing Dates
- Using the Current Date the U.S./U.K./European Way
- Formatting a Specific Date
- Validating a Date
- Calculating a Relative Date
- Creating a Sortable Time Stamp
- Converting a String into a Date
- Determining Sunrise and Sunset
- Using Date and Time for Benchmarks
- Using Form Fields for Date Selection
- Create Self-updating Form Fields for Date Selection
- Calculating the Difference Between Two Dates
- Using GMT Date/Time Information
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)