NYCPHP Meetup

NYPHP.org

[nycphp-talk] Localizing Time Zones

Hans Zaunere hans at nyphp.org
Tue Aug 12 16:33:04 EDT 2003



D C Krook wrote:

> We've been looking for a good way to customize a web-based work flow 
> tool so that each user who logs in sees the time stamp of events in his 
> own time zone, not that of the server.
> 
> For example, a developer in India who is given a task can see when it 
> was assigned by the project manager in New York displayed in Bangalore 
> time.  Similarly the PM would see when the work was done relative to New 
> York time.  Both users would see GMT as a frame of reference as well.
> 
> To implement this functionality, we've taken a look at Adam and David's 
> PHP Cookbook where they address this very problem (Chapter 3, 
> Calculating Time with Time Zones).
> 
> Their solution involves the temporary change of the TZ environment 
> variable:
> 
>   "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."
> 
>   function pc_mktime($tz,$hr,$min,$sec,$mon,$day,$yr) {
>       putenv("TZ=$tz");
>       $a = mktime($hr,$min,$sec,$mon,$day,$yr);
>       putenv('TZ=EST5EDT');   // change EST5EDT to your server's time zone!
>       return $a;
>   }

This would work, but also remember that UNIX timestamps aren't timezone aware themselves.  Timezones only come into effect when you convert from the timestamp to a human readable date and time.

http://www.gnu.org/manual/glibc-2.2.5/html_node/Simple-Calendar-Time.html#Simple%20Calendar%20Time

in which case:

http://us4.php.net/manual/en/function.strftime.php
http://us4.php.net/manual/en/function.setlocale.php

might come in handy at some point.

> This seems to be the solution we're after, but we're concerned about 
> whether this is thread safe, particularly in the context of a heavy load 
> server with lots of users redefining the TZ environment variable 
> simultaneously.

Anywho, do you really mean thread-safe?  Unless you're running PHP under a threaded MPM in Apache 2, or IIS or something else, you won't need to worry about threads.  Each Apache child is a full process, and thus has it's own environment (ie, it's own instance of the TZ environment variable) so you should be fine.


H




More information about the talk mailing list