Recipe 9.18. Creating Drop-Down Menus Based on the Current Date


9.18.1. Problem

You want to create a series of drop-down menus that are based automatically on the current date.

9.18.2. Solution

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

<?php list($hour, $minute, $second, $month, $day, $year) =                                   split(':', date('h:i:s:m:d:Y')); // print out one week's worth of days for ($i = 0; $i < 7; ++$i) {     $timestamp = mktime($hour, $minute, $second, $month, $day + $i, $year);     $date = date("D, F j, Y", $timestamp);     print "<option value='$timestamp'>$date</option>\n"; } ?> 

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>

9.18.3. Discussion

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

<?php $timestamp = mktime(0, 0, 0, 10, 30, 2008); // October 30, 2008 $one_day = 60 * 60 * 24; // number of seconds in a day // print out one week's worth of days for ($i = 0; $i < 7; ++$i) {     $date = date("D, F j, Y", $timestamp);     print "<option value='$timestamp'>$date</option>\n";     $timestamp += $one_day; } ?> 

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.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net