Recipe 3.12. Accounting for Daylight Savings Time


3.12.1. Problem

You need to make sure your time calculations properly consider DST.

3.12.2. Solution

If you're using PHP 5.1.0 or later, set the appropriate time zone with date_default_timezone_set( ). These time zones are DST-aware. Example 3-34 uses date_default_timezone_set( ) to print out an appropriately DST-formatted time string.

Handling DST with date_default_timezone_set( )

<?php // Denver, Colorado observes DST date_default_timezone_set('America/Denver'); // July 4, 2008 is in the summer $summer = mktime(12,0,0,7,4,2008); print date('c', $summer) . "\n"; // Phoenix, Arizona does not observe DST date_default_timezone_set('America/Phoenix'); print date('c', $summer) . "\n"; ?>

Example 3-34 prints:

2008-07-04T12:00:00-06:00 2008-07-04T11:00:00-07:00

With an earlier version of PHP, you must use another method. The zoneinfo library calculates the effects of DST properly. If you are using a Unix-based system, take advantage of zoneinfo with putenv( ), as shown in Example 3-35.

Handling DST with zoneinfo

<?php // Denver, Colorado observes DST putenv('TZ=America/Denver'); // July 4, 2008 is in the summer $summer = mktime(12,0,0,7,4,2008); print date('c', $summer) . "\n"; // Phoenix, Arizona does not observe DST putenv('TZ=America/Phoenix'); print date('c', $summer) . "\n"; ?>

If you can't use zoneinfo, you can modify hardcoded time zone offsets based on whether the local time zone is currently observing DST. Use localtime( ) to determine the current DST observance status, as shown in Example 3-36.

Handling DST with explicit offsets

<?php // Find the current UTC time $now = time(); // California is 8 hours behind UTC $now -= 8 * 3600; // Is it DST? $ar = localtime($now,true); if ($ar['tm_isdst']) { $now += 3600; } // Use gmdate() or gmstrftime() to print California-appropriate time print gmstrftime('%c',$now); ?>

3.12.3. Discussion

Altering an epoch timestamp by the amount of a time zone's offset from UTC and then using gmdate( ) or gmstrftime( ) to print out time zoneappropriate functions is flexible'it works from any time zone'but the DST calculations are slightly inaccurate. For the brief intervals when the server's DST status is different from that of the target time zone, the results are incorrect. For example, at 3:30 A.M. EDT on the first Sunday in April (after the switch to DST), it's still before the switch (11:30 P.M.) in the Pacific time zone. A server in Eastern time using this method calculates California time to be seven hours behind UTC, whereas it's actually eight hours. At 6:00 A.M. EDT (3:00 A.M. PDT), both Pacific and Eastern time are observing DST, and the calculation is correct again (putting California at seven hours behind UTC).

3.12.4. See Also

Recipe 3.11 for dealing with time zones; documentation on date_default_timezone_set( ) at http://www.php.net/date_default_timezone_set, on putenv( ) at http://www.php.net/putenv, localtime( ) at http://www.php.net/localtime, gmdate( ) at http://www.php.net/gmdate, and gmstrftime( ) at http://www.php.net/gmstrftime; a detailed presentation on DST is at http://webexhibits.org/daylightsaving/.




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