PHP Calculate time difference between countries v2


Notes From Kuku

Previous code wasn’t bad but geoip_time_zone_by_country_and_region() has limitations.

geoip_time_zone_by_country_and_region() CONS:

  • PECL extension
  • Can not calculate timezone for every country
  • You must use country and area to calculate time zone*

* Some countries have more than 1 time zone such as Indonesia (4 time zones) and you must give area name together with country code to calculate each time zone

So we’ll change the previous code and use DateTimeZone::listIdentifiers() function

DateTimeZone::listIdentifiers() PROS:

  • Built in to php core
  • Retrieves every possible time zone for a country
  • Works for every country

So here is the code:

Origin country: New York/United States
Destination Country: Indonesia

First we will retrieve each timezone within Indonesia as an array:

$timezones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, "ID");

Then we will find offset between New York and each time zone in Indonesia. We will use get_timezone_offset() custom function (that i mentioned in the previous post) to find offset:

function…

View original post 248 more words

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s