Software Developer

Get Timezone By Latitude/Longitude Coordinates From GeoNames With PHP

This is a PHP function to get the timezone id for a set of latitude/longitude coordinates from GeoNames JSON webservice. (See the second example for how to do this in WordPress.) The latitude and longitude must be passed as decimals. To make it work, you need to replace demo at the end of line 8 with your own username from Geonames.org. They require this. (See comments below for the link to the “create new account” page.)

/** 
  * Get timezone id by coordinates from GeoNames JSON webservice
  *
  * @param float $latitude The latitude decimal degrees
  * @param float $longitude The longitude decimal degrees
  */
function getTimezoneGeo($latitude, $longitude) {
	$json = file_get_contents("http://api.geonames.org/timezoneJSON?lat=".$latitude."&lng=".$longitude."&username=demo");
	$data = json_decode($json);

	// check for GeoNames exceptions
	if (! empty($data->status)) {
		return 'ERROR '.$data->status->value.' - '.$data->status->message;
	}
	
	return $data->timezoneId;
}

Get GeoNames Timezone ID in WordPress

This is a function you can use in WordPress to get a timezone ID from GeoNames webservices, based on city coordinates. The latitude and longitude must be passed as decimals. You must replace demo on line 9 with your own username from Geonames.org.

/**
 * Get Geonames Timezone ID based on latitude/longitude coordinates
 * @param float $latitude, The latitude decimal
 * @param float $longitude, The longitude decimal
 */
function my_get_geonames_timezone_id( $latitude, $longitude ) {

	$api_params = array(
		'username'	=> 'demo',
		'lat'		=> $latitude,
		'lng'		=> $longitude,
	);

	$request = wp_remote_post( 'http://api.geonames.org/timezoneJSON', array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );

	if ( ! is_wp_error( $request ) ) {
		$request = json_decode( wp_remote_retrieve_body( $request ) );
	} else {
		return false;
	}

	// check for GeoNames exceptions
	if ( ! empty( $request->status ) ) {
		return 'ERROR ' . $request->status->value . ' - ' . $request->status->message;
	}

	return $request->timezoneId
}

See more: , ,

By

We've 6 Responses

  1. January 17th, 2014 at 12:52 am

    Hello Isabel,

    {“status”:{“message”:”the daily limit of 30000 credits for demo has been exceeded. Please use an application specific account. Do not use the demo account for your application.”,”value”:18}}

    am getting this kind of message. Could you please help?

    avatar
  2. May 24th, 2016 at 11:05 am

    Thank you for this post. Unbelievably, the Xamarin guide to writing a REST WebServices app, for total beginners, assumes that you already know not to use the demo account…

    avatar
  3. July 24th, 2019 at 6:28 pm

    Thank you for your example, easy to understand but sadly didn’t work on my hosted server. Most of them do not allow (anymore) file open over http/s. The answer for this is Curl. Here’s a fully working example (set latitude, longitude and your registered username of geonames.org):

        curl_setopt ($ch, CURLOPT_URL, "http://api.geonames.org/timezoneJSON?lat=".<latitude>."&lng=".<longitude>."&username=demo");
    
    $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, []);
        curl_setopt($ch, CURLOPT_HTTPGET, true);
        curl_setopt ($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
        $result = json_decode(curl_exec ($ch), true);
        print_r ($result);
    
    curl_close ($ch);
    
    

    I hope this may help someday anyone else. ;o)

    Regards

    avatar

Leave a Reply to javarose Cancel reply

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]