geo.php 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Georg Ehrke <ownclouddev at georgswebsite dot de>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class OC_Geo{
  9. /*
  10. * @brief returns the closest timezone to coordinates
  11. * @param (string) $latitude - Latitude
  12. * @param (string) $longitude - Longitude
  13. * @return (string) $timezone - closest timezone
  14. */
  15. public static function timezone($latitude, $longitude) {
  16. $alltimezones = DateTimeZone::listIdentifiers();
  17. $variances = array();
  18. //calculate for all timezones the system know
  19. foreach($alltimezones as $timezone) {
  20. $datetimezoneobj = new DateTimeZone($timezone);
  21. $locationinformations = $datetimezoneobj->getLocation();
  22. $latitudeoftimezone = $locationinformations['latitude'];
  23. $longitudeoftimezone = $locationinformations['longitude'];
  24. $variances[abs($latitudeoftimezone - $latitude) + abs($longitudeoftimezone - $longitude)] = $timezone;
  25. }
  26. //sort array and return the timezone with the smallest difference
  27. ksort($variances);
  28. reset($variances);
  29. return current($variances);
  30. }
  31. }