tags:

views:

212

answers:

1

I wrote a pretty long function that will calculate the differences in timezones across the U.S. relative to the server timezone. I have noticed that by using default_timezone_set() that I can get the same data back that my function calculates. I have chosen not to use the default_timezone_set function because I am afraid that this changes the time on a global level and that if another request somewhere else is occurring within my app, that the wrong time will be used; that is, the time that is being calculated at that moment using the default_timezone_set function.

Can someone verify this for me? Can I just use the timezone_set function without having to worry about the possibility of incorrect times being used in other functions? Hope this is clear.

+1  A: 

You should take a look at how PHP determines what timezone to use in the absence of date_default_timezone_set(). It's described under the date_default_timezone_get() function.

Basically, if you're not using date_default_timezone_set(), then PHP is most likely using the server timezone as the default timezone for all the date functions anyway, unless you happened to have set one of the other settings listed under date_default_timezone_get().

If you specifically set the timezone using date_default_timezone_set(), then you are making sure of the timezone that your code will use, rather than letting the server timezone decide for you.

If you want to use the server timezone, then it's good practice to simply set the timezone to the server timezone using date_default_timezone_set(), and all ambiguity is removed.

So, it's too late for the short answer, but the short answer is yes, you should be able to use date_default_timezone_set() safely.

zombat
Hi Zombat. I'm already using set_timzone to set the server timezone. All of my data is timestamped with the server time. I'm trying to calculate the differences in the timezone per user. The timezone_set function will successfully return the difference or the correct time based on what the user wants but I'm afraid that I may be overriding my server time by doing this.
jim
Hmm... I think you'll have to be clearer. There's no such function as `set_timezone()` or `timezone_set()`. What are the functions you are using? If you're using `date_timezone_set()`, then that applies on an object level to whatever DateTime object you might be using, and won't affect the global settings.
zombat