1

Possible Duplicate:
Daylight saving time and Timezone best practices

Basically I have the following problem:

I have a table with all the GMT offsets and based on that I calculated the users hour relative to the servers hour but some are off by an hour.

So my function looks like this, it takes GMT+/-3600*usersTZ(GMT offset). London looks ok, Australia looks ok, but hey Samoa, instead of being eg: 5 it`s 6.

I figured that might be the case with daylight saving, any ideeas?

Community
  • 1
  • 1
Gabriel
  • 772
  • 1
  • 13
  • 37
  • [PHP's DateTime and strtotime are DST aware](http://stackoverflow.com/questions/2505681/timezone-conversion-in-php/2505687#2505687). Use them instead. – Gordon Sep 20 '11 at 07:53
  • Thanks Gordon, but please ask questions before answering like a forum robot :p. I've already checked the above topics, strtotime is not working, Samoa time is not showing correctly for 'Pacific/Samoa' timezone in strtotime, now it should be 9:01pm Monday (WST), which I think is rather akward because Samoa is GMT -11 which makes it 10pm. And besides, I need to do that based on the TZ of each client – Gabriel Sep 20 '11 at 08:04
  • then I wonder why you accepted the answer that suggested what I suggested right from the start? – Gordon Sep 20 '11 at 12:29

1 Answers1

2

PHP can handle timezones natively. You just need to set timezone in olson format like America/New_York.

You can then use date_default_timezone_set or any other php DateTime stuff that understands timezones.

It would be very helpful to peruse the PHP docs a little. Especially here and here.

blockhead
  • 9,655
  • 3
  • 43
  • 69
  • Yes but wouldn`t that change all the date and time behaviour of all date and time functions in the entire script, where I can only modify only one function and besides, this is not my server and changing functionality to suite my needs as a developer is not an option. – Gabriel Sep 20 '11 at 07:59
  • That was just an example of a simple way to do it. You can use the `DateTime` class instead which takes a `DateTimeZone` object as a parameter. As I said before, I _strongly_ recommend you read through the docs. – blockhead Sep 20 '11 at 08:11
  • Tried that, but it says it does not recognize the class and I`m afraid that means the PHP wasn`t compiled with that in mind. Don`t have the luxury of recompiling PHP. – Gabriel Sep 20 '11 at 08:14
  • What version of PHP are you using? This has been built in since version 5.2.0... – blockhead Sep 20 '11 at 08:17
  • Thanks, problem solved :), you`ve got my vote – Gabriel Sep 20 '11 at 08:34