Question

I'm using http://ipinfodb.com/ api in order to get users lat/long... I have users timezone in the format of an array. The array returns the following information...

array(11) { 
     ["statusCode"]=> string(2) "OK"... 
     ["countryCode"]=> string(2) "CA" 
     ["countryName"]=> string(6) "CANADA" 
     ["regionName"]=> string(16) "BRITISH COLUMBIA" 
     ["cityName"]=> string(8) "VANCOUVER" ....
     ["timeZone"]=> string(6) "-07:00" 
}

With the information from the array, how would i find the users current time when i have their timezone in the format of -07:00, 04:00... etc etc

Was it helpful?

Solution

A "time zone" is NOT an offset! It bothers me that so many APIs make this common mistake. Don't be fooled by the fact that the return field is called "timeZone".

A "time zone" is an region that can have one or more offsets throughout the year. You can resolve an offset from a time zone and a date, but you cannot go the other direction! A list of TZDB time zones is here.

The specific time zone would be America/Vancouver - not because your lat/lon matched the city of Vancouver, but because Vancouver happens to be the reference city for the time zone of your location. Not every city has its own time zone id, so you can't make any assumptions.

-07:00 is the current offset for the America/Vancouver time zone. It happens to be the daylight savings offset. When daylight savings ends, it will change to -08:00. It would appear that the particular web service you are using does not reveal this information.

There are several other questions on StackOverflow that already answer how to resolve an actual time zone from a lat/lon. There are web services you can call, or you can use your own database. Here are some links:

I'm sure there are more. I was tempted to close this as a duplicate, but I hadn't seen ipinfodb.com referenced before.

If you are certain that your API returns the current offset, and you just want the user's current time, then as Jonathon pointed out in the question comments, you can just apply the offset to the current UTC time and you'll know what the time is in that location. But you'll have to do this every time. You can't save that offset and use it again later. For that, you need the full time zone reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top