You need to calculate times in different time zones. For example, you want to give users information adjusted to their local time, not the local time of your server.
For simple calculations, you can explicitly add or subtract the offsets between two time zones, as in Example 3-27.
Simple time zone calculation
In PHP 5.1.0 and later, use date_default_timezone_set( ) to adjust the time zone that PHP uses. Example 3-28 prints the current time twice'once as appropriate for New York and once for Paris.
Changing time zone with date_default_timezone_set( )
On Unix-based systems with earlier versions of PHP, if you don't know the offsets between time zones, just set the TZ environment variable to your target time zone, as in Example 3-29.
Changing time zone with an environment variable
Before we sink too deeply into the ins and outs of time zones, we want to pass along the disclaimer that the U.S. Naval Observatory offers at http://tycho.usno.navy.mil/tzones.html. Namely, official worldwide time zone information is somewhat fragile "because nations are sovereign powers that can and do change their timekeeping systems as they see fit." So, remembering that we are at the mercy of the vagaries of international relations, here are some ways to cope with Earth's many time zones.
The time and date functions were overhauled in PHP 5.1.0, and one of the best parts of that overhaul was greatly improved time zone handling. The added date_default_timezone_get( ) and date_default_timezone_set( ) functions make it a breeze to twiddle time zones to get appropriately formatted output. There is also a new configuration directive, date.timezone, that sets the default time zone to use if you don't call date_default_timezone_set( ).
With these functions available, all you have to do before generating a formatted time or date string with date( ) or strftime( ) is make sure that the currently set default time zone (either from date.timezone or date_default_timezone_set( )) is the one you want to use. If you're building an app that is used by people in multiple time zones, a handy technique is to make the default time zone GMT and then explicitly set the appropriate time zone (based, perhaps, on user preference) before creating a date or time string. This makes it clear in your code that you're generating a time zonespecific value.
The time zones that PHP understands are listed in Appendix H of the PHP Manual (http://www.php.net/timezones). The names of these time zones'such as America/New_York, Europe/Paris, and Africa/Dar_es_Salaam'mirror the structure of the popular zoneinfo database.
If you're using an earlier version of PHP, you have to do the time zone math yourself. For a relatively simple treatment of offsets between time zones, use an array in your program that has the various time zone offsets from UTC. Once you determine what time zone your user is in, just add that offset to the appropriate UTC time and the functions that print out UTC time (e.g., gmdate( ), gmstrftime( )) can print out the correct adjusted time. Example 3-30 adjusts the time from UTC to PST.
Adjusting time from UTC to another time zone
Example 3-30 uses the $pc_timezones array defined in Example 3-31, which contains offsets from UTC.
Offsets from UTC
On Unix systems, you can use the zoneinfo library to do the conversions. This makes your code more compact and also transparently handles DST , as discussed in Recipe 3.12.
To take advantage of zoneinfo in PHP, do all your internal date math with epoch timestamps. Generate them from time parts with the pc_mktime( ) function shown in Example 3-32.
Calling putenv( ) before mktime( ) fools the system functions mktime( ) uses into thinking they're in a different time zone. After the call to mktime( ), the correct time zone has to be restored. On the East Coast of the United States, that's EST5EDT. Change this to the appropriate value for your computer's location (see Table 3-5). Manipulating environment variables, however, can cause problems in multithreaded environments. If you're using PHP with a multithreaded web server, it is an extremely good idea to upgrade to at least PHP 5.1.0, so you can use date_default_timezone_set( ).
Time parts are turned into epoch timestamps by pc_mktime( ). Its counterpart, which turns epoch timestamps into formatted time strings and time parts, is pc_strftime( ), shown in Example 3-33.
Example 3-33 uses the same system functionfooling pc_mktime( ) does to get the right results from strftime( ).
The great thing about these functions is that you don't have to worry about the offsets from UTC of different time zones, whether DST is in effect, or any other irregularities of time zone differences. You just set the appropriate zone and let your system libraries do the rest.
Note that the value of the $tz variable in both these functions should not be a time zone name but a zoneinfo zone. zoneinfo zones are more specific than time zones because they correspond to particular places. Table 3-5 contains mappings for appropriate zoneinfo zones for some UTC offsets. The last column indicates whether the zone observes DST.
Countries around the world don't begin and end DST observance on the same days or at the same times. To calculate time appropriately for an international DSTobserving location, pick a zoneinfo zone that matches your desired location as specifically as possible.
3.11.4. See Also
Recipe 3.12 for dealing with DST; documentation on date_default_timezone_set( ) at http://www.php.net/date_default_timezone_set, on date_default_timezone_get( ) at http://www.php.net/date_default_timezone_get, on putenv( ) at http://www.php.net/putenv, on localtime( ) at http://www.php.net/localtime, on gmdate( ) at http://www.php.net/gmdate, and on gmstrftime( ) at http://www.php.net/gmstrftime; the time zones that PHP knows about are listed at http://www.php.net/timezones; zoneinfo zone names and longitude and latitude coordinates for hundreds of places around the world are available at ftp://elsie.nci.nih.gov/pub/'look for the most recent file whose name begins with tzdata; many links to historical and technical information about time zones, as well as information on the zoneinfo database, can be found at the following address: http://www.twinsun.com/tz/tz-link.htm.