Archive

Archive for May, 2013

PHP Calculate time difference between countries v2

May 2, 2013 1 comment

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 get_timezone_offset($remote_tz, $origin_tz = null) {
    if ($origin_tz === null) {
        if (!is_string($origin_tz = date_default_timezone_get()))
             return false;
    }
    $origin_dtz = new DateTimeZone($origin_tz);
    $remote_dtz = new DateTimeZone($remote_tz);
    $origin_dt = new DateTime("now", $origin_dtz);
    $remote_dt = new DateTime("now", $remote_dtz);
    $offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);

    return $offset;
}

So lets put these 2 functions together and save time in destination country, timezone of destination country and hour difference in a array then print results.

Here is the complete code ready to copy and paste:

function get_timezones($country) {
$i = 0;
$timezones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $country);

foreach ($timezones as $timezone) {
    $offset = get_timezone_offset($timezone, 'America/New_York');
    $offset_time = time() - $offset;

    $dr[$i]['timeincountry'] = date('Y-m-d H:i', $offset_time);
    $dr[$i]['timezone'] = $timezone;

    $of = $offset / 3600;

    if ($timezone) {
        if ($of > 0) {
            $dr[$i]['hourdifference'] = abs($of) . " hours behind";
        } elseif ($of < 0) {
            $dr[$i]['hourdifference'] = abs($of) . " hours ahead";
        } else {
            $dr[$i]['hourdifference'] = "There is no time difference";
        }
    }
    $i++;
}
}

function get_timezone_offset($remote_tz, $origin_tz = null) {
    if ($origin_tz === null) {
        if (!is_string($origin_tz = date_default_timezone_get()))
             return false;
    }
    $origin_dtz = new DateTimeZone($origin_tz);
    $remote_dtz = new DateTimeZone($remote_tz);
    $origin_dt = new DateTime("now", $origin_dtz);
    $remote_dt = new DateTime("now", $remote_dtz);
    $offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);

    return $offset;
}

// Calculate time differences between new york and Indonesia then print out the results

$timezones = get_timezones("ID");

foreach ($timezones as $tz) {
    $content.= '<div id="boxcorner">';
    $content.= $tz['timezone'] . '</br>';
    $content.= $tz['timeincountry'] . '</br>';
    $content.= $tz['hourdifference'];
    $content.= '</div>';
}

echo $content;
Advertisements