function geotimezone_query in Geo Time Zone 7.2
Same name and namespace in other branches
- 8.3 geotimezone.module \geotimezone_query()
- 8 geotimezone.module \geotimezone_query()
- 8.2 geotimezone.module \geotimezone_query()
- 6.2 geotimezone.module \geotimezone_query()
- 6 geotimezone.module \geotimezone_query()
- 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 59 - 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'])) {
module_load_include('php', 'geotimezone', 'src/TimezoneInterface');
module_load_include('php', 'geotimezone', 'src/Timezone');
module_load_include('php', 'geotimezone', 'src/TimezonePolygon');
module_load_include('php', 'geotimezone', 'src/GeoTimezone');
$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'])) {
module_load_include('php', 'geotimezone', 'src/TimezoneInterface');
module_load_include('php', 'geotimezone', 'src/RegionTimezone');
$geotimezone = new \Drupal\geotimezone\RegionTimezone($location);
}
else {
module_load_include('php', 'geotimezone', 'src/TimezoneInterface');
module_load_include('php', 'geotimezone', 'src/CountryTimezone');
$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(),
];
}
}