You want to create a series of drop-down menus that are based automatically on the current date.
Use date( ) to find the current time in the web server's time zone and loop through the days with mktime( ) .
Example 9-31 generates <option/> values for today and the six days that follow. In this case, "today" is December 3, 2008.
Generating date-based drop-down menu options
When run on December 3, 2008, Example 9-31 prints:
<option value='1228305600'>Wed, December 3, 2008</option> <option value='1228392000'>Thu, December 4, 2008</option> <option value='1228478400'>Fri, December 5, 2008</option> <option value='1228564800'>Sat, December 6, 2008</option> <option value='1228651200'>Sun, December 7, 2008</option> <option value='1228737600'>Mon, December 8, 2008</option> <option value='1228824000'>Tue, December 9, 2008</option>
In Example 9-31 we set the value for each date as its Unix timestamp representation because we find this easier to handle inside our programs. Of course, you can use any format you find most useful and appropriate.
Don't be tempted to eliminate the calls to mktime( ); dates and times aren't as consistent as you'd hope. Depending on what you're doing, you might not get the results you want. Example 9-32 takes the shortcut of just incrementing the timestamp by the number of seconds in each day (60 seconds per minute x 60 minutes per hour x 24 hours per day = 86,400 seconds).
Incorrectly generating date-based drop-down menu options
Example 9-32 prints:
<option value='1225339200'>Thu, October 30, 2008</option> <option value='1225425600'>Fri, October 31, 2008</option> <option value='1225512000'>Sat, November 1, 2008</option> <option value='1225598400'>Sun, November 2, 2008</option> <option value='1225684800'>Sun, November 2, 2008</option> <option value='1225771200'>Mon, November 3, 2008</option> <option value='1225857600'>Tue, November 4, 2008</option>
Example 9-32 should print out the month, day, and year for a seven-day period starting October 30, 2008. However, it doesn't work as expected.
Why are there two Sun, November 2, 2008 in the list? The answer: daylight saving time. It's not true that the number of seconds in a day stays constant; in fact, it's almost guaranteed to change. Worst of all, if you're not near either of the changeover dates, you're liable to miss this bug during testing.
9.18.4. See Also
Chapter 3, particularly Recipe 3.12, but also Recipes 3.2, 3.3, 3.12, 3.5, 3.6, 3.12, and 3.14; documentation on date( ) at http://www.php.net/date and mktime( ) at http://www.php.net/mktime.