Get Local Time by Address using Earth tools and Google maps(Location To Time PHP)

Context:

Once I was needed to fetch correct time of the location by it’s address. My project was a portal application and the user should be facilitated with the local time of other users. so I have made a function that can calculate correct time  of the address supplied.

Implementation:

This function uses two APIS

1) Google maps

2) Earth tools

By calling Google maps I have fetched lat & lng of the location and passed them to earth tools.

Earth tool  provides the correct time at the lat&lng supplied.

/*@param $address (string) comma separated address (full or partial)

*/

public function GetLocalTime($address)
{
// fetching lat&lng from Google Maps
$request_uri = 'http://maps.googleapis.com/maps/api/geocode/xml?address='.urlencode($address).'&sensor=true';
$google_xml = simplexml_load_file($request_uri);
$lat = (string)$google_xml->result->geometry->location->lat;
// fetching time from earth tools
$lng = (string)$google_xml->result->geometry->location->lng;
$earth_uri  = "http://www.earthtools.org/timezone/$lat/$lng";
$earth_response = simplexml_load_file($earth_uri);
return (string)$earth_response->localtime;
}

echo GetLocalTime('Ahmedabad,Gujarat');

Let me know if it is useful to you or have any query, Thanks for reading.

Regards,

Rupesh Patel

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s