You are here

geotimezone.module in Geo Time Zone 8.3

API that determines the time zone based on geo location (coordinates or region or country). Using "geotimezone_query($location, $format);" that returns time zone identifier or UTC/GMT offset or both (depends on $format value). Where the first parameter is an associative array with array keys of: longitude latitude regionCode region countryCode To determine the time zone based on coordinates, the 'longitude' and 'latitude' keys are required to have valid coordinates values. To determine the time zone based on region, the 'countryCode' and either of 'regionCode' or 'region' are required. To determine the time zone based on country, the 'countryCode' is required. If all keys are present, the coordinates (longitude/latitude) are the query's priority (the rest will be ignored). The region is the next query's priority if coordinates are not present. And the country is the last priority if coordinates and region are not present. It is recommended to provide coordinates for accurate time zone while region is the next accurate and the least is the country where it may return multiple time zones in array.

This module does not need web services, data files or SQL database. This is a Drupal version of LatLongToTimezone project (https://github.com/drtimcooper/LatLongToTimezone)

@author Roland Michael dela Peña.

File

geotimezone.module
View source
<?php

/**
 * @file
 * API that determines the time zone based on geo location (coordinates or
 * region or country). Using "geotimezone_query($location, $format);" that
 * returns time zone identifier or UTC/GMT offset or both (depends on $format
 * value). Where the first parameter is an associative array with array keys of:
 * longitude
 * latitude
 * regionCode
 * region
 * countryCode
 * To determine the time zone based on coordinates, the 'longitude' and
 * 'latitude' keys are required to have valid coordinates values.
 * To determine the time zone based on region, the 'countryCode' and either of
 * 'regionCode' or 'region' are required.
 * To determine the time zone based on country, the 'countryCode' is required.
 * If all keys are present, the coordinates (longitude/latitude) are the query's
 * priority (the rest will be ignored). The region is the next query's priority
 * if coordinates are not present. And the country is the last priority if
 * coordinates and region are not present.
 * It is recommended to provide coordinates for accurate time zone while region
 * is the next accurate and the least is the country where it may return
 * multiple time zones in array.
 *
 * This module does not need web services, data files or SQL database. This is a
 * Drupal version of LatLongToTimezone project
 * (https://github.com/drtimcooper/LatLongToTimezone)
 *
 * @author Roland Michael dela Peña.
 */

/**
 * Query time zone based on geo location.
 *
 * @param array $location
 *   Location details in an associative array with array keys of:
 *   longitude
 *   latitude
 *   regionCode
 *   region
 *   countryCode
 *   To determine the time zone based on coordinates, the 'longitude' and
 *   'latitude' keys are required to have valid coordinates values.
 *   To determine the time zone based on region, the 'countryCode' and either of
 *   'regionCode' or 'region' are required.
 *   To determine the time zone based on country, the 'countryCode' is required.
 *   If all keys are present, the coordinates (longitude/latitude) are the
 *   query's priority (the rest will be ignored). The region is the next query's
 *   priority if coordinates are not present. And the country is the last
 *   priority if coordinates and region are not present.
 * @param string $format
 *   Desired return time zone format: 'identifier', 'offset' or 'both'
 * @return mixed
 *   Query result contains time zone identifier or offset or both. NULL if not
 *   found or no valid location fields present
 */
function geotimezone_query($location, $format = 'both') {
  if (isset($location['latitude']) && !empty($location['latitude']) && isset($location['longitude']) && !empty($location['longitude'])) {
    $geotimezone = new \Drupal\geotimezone\GeoTimezone($location['latitude'], $location['longitude']);
  }
  elseif (isset($location['countryCode']) && !empty($location['countryCode'])) {
    if (isset($location['regionCode']) && !empty($location['regionCode']) || isset($location['region']) && !empty($location['region'])) {
      $geotimezone = new \Drupal\geotimezone\RegionTimezone($location);
    }
    else {
      $geotimezone = new \Drupal\geotimezone\CountryTimezone($location);
    }
  }
  else {

    // No valid location fields present.
    return NULL;
  }
  if ($format == 'identifier') {
    return $geotimezone
      ->getIdentifier();
  }
  elseif ($format == 'offset') {
    return $geotimezone
      ->getOffset();
  }
  else {
    return [
      'identifier' => $geotimezone
        ->getIdentifier(),
      'offset' => $geotimezone
        ->getOffset(),
    ];
  }
}

Functions

Namesort descending Description
geotimezone_query Query time zone based on geo location.