3.12.1. ProblemYou need to make sure your time calculations properly consider DST. 3.12.2. SolutionIf 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( )
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
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
3.12.3. DiscussionAltering 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 AlsoRecipe 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/. |