2

I have this data available for use:

Timezone of local time as integer: 8.75 (which is UTC+8.75)
1) 2012-09-04 03:15:03
2) 2012-09-03 18:30:03 (GMT)
3) 1346728503
4) 1346697003 (GMT)

Now, I'm wondering how to convert local time (1) to GMT one (2) (and vice versa) with consideration of all possible exceptions like summer times, 1970-2038 time limits etc.? Do they matter anyway?

PHP functions mainly don't allow to specify timezone of local time to convert times - they take server's local time which is not what I want (and I don't want to alter it either). I can obviously subtract 8.75*60*60 from 1346728503 (3) but will this produce accurate value no matter what?

Atadj
  • 7,050
  • 19
  • 69
  • 94

3 Answers3

3

OK, first: PHP functions that accept timestamps don't accept timezones because UNIX timestamps don't know or care about time zones.

As I type, it is Monday, September 3rd, 2012 at 15:30:10 in the US Eastern time zone, which is currently observing Daylight Saving Time ("summer time"). In the UTC time zone, that corresponds to 19:30:10. Either way, it's UNIX timestamp (time_t value) 1,346,700,610. Depending upon your location on the Earth's surface, your local time at that instant could fall anywhere in a 26-hour range, from 7:30:10 AM on September 3rd to 9:30:10 AM on September 4th (2012-09-03T07:30:10-12:00 to 2012-09-04T09:30:10+14:00). But no matter what your local label, the time in question is still 1,346,700,610 to UNIX and PHP.

It looks like you attempted to calculate different time_t values based on time zone, but that doesn't compute. Time zone offsets are never applied to time_t values, which don't change; they are only applied to the human-readable representation.

If at all possible, do not attempt to do your own time zone conversions. The rules tend to change. Right now, local time for me is four hours earlier than UTC; in a couple months, it will be 5 hours earlier. Moreover, the rules for when that change happens are different this year than they were five years ago. So depending on the specific time stamp value I'm trying to convert, I have to apply different rules.

Use the library functions.

To convert a string representation of a time into a time_t value, use strtotime. If the string contains a time zone reference, strtotime will do the conversion for you. If not, you can use date_default_timezone_set, as Connie suggested, to tell PHP what timezone to use when interpreting the string.

To convert a time_t value back into a string, use date to get "local" time (that is, time according to the time zone set with date_default_timezone_set; you can ask PHP what that timezone is with date_default_timezone_get), or gmdate to get UTC.

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • Thanks for your extensive explanations! :) I'm just running into some odd issues like 1969 December 31 15:15 in timestamp GMT mysql table (which should not be possible?) and all those timezones are problematic. I think I'll just leave it like that with the most basic calculations. I need to replicate WordPress' time converter but it's buggy, too... you can't for instance use 1876 as blog post creation date. It will be 1876 in one database table but GMT one will stop being accurately synchronized. – Atadj Sep 03 '12 at 19:49
  • I added some function refs that might help. Time 0 is midnight UTC on Jan 1, 1970, which in other time zones might show up as late as 2pm on that date or as early as noon the previous date (Dec 31, 1969). Some applications will handle negative timestamps and let you go further back. – Mark Reed Sep 03 '12 at 19:55
1

You have it built in already with the gmdate() to get the date/time in GMT and gmstrftime() to convert from a given date/time string.

Or you could use the timezone_offset_get() and do it manually.

Clarence
  • 2,944
  • 18
  • 16
0

Are you looking for this?

date_default_timezone_set('America/Denver');

For a full list of timezones that will work with this function, click here

Connie
  • 77
  • 3
  • 8
  • I'm looking for a way to change (1) into (2) from my list with consideration of various exceptions. – Atadj Sep 03 '12 at 19:26