phpiostimezonewebapinstimezone

Get timezone from an iOS device requesting a web API in PHP


I am developing an API for a iOS app. I need to get the timezone of the iPhone that is requesting the web API.

The normal way is to get it by fetching the IP when the user loads the page, but how do I get the timezone for an iOS device connecting my app ?


Solution

  • You'll need to first get an NSTimeZone object using the iOS functions - either systemTimeZone or localTimeZone (see here for differences). The name property of the NSTimeZone object should give you back a string with a standard IANA TZ database identifier, such as "America/New_York". That's what you send to your web service. The fact that your web service is written in PHP does not matter from the iOS environment.

    However, these time zone identifiers are indeed the same ones used by PHP, as seen in the list of support time zones. You can use the with functions like date_default_timezone_set, or with a DateTimeZone object that you can pass when working with a DateTime.