You are here

function geotimezone_query in Geo Time Zone 8.3

Same name and namespace in other branches
  1. 8 geotimezone.module \geotimezone_query()
  2. 8.2 geotimezone.module \geotimezone_query()
  3. 6.2 geotimezone.module \geotimezone_query()
  4. 6 geotimezone.module \geotimezone_query()
  5. 7.2 geotimezone.module \geotimezone_query()
  6. 7 geotimezone.module \geotimezone_query()

Query time zone based on geo location.

Parameters

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.

string $format: Desired return time zone format: 'identifier', 'offset' or 'both'

Return value

mixed Query result contains time zone identifier or offset or both. NULL if not found or no valid location fields present

File

./geotimezone.module, line 60
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…

Code

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(),
    ];
  }
}