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


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.


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 = ''.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  = "$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.


Rupesh Patel


Leave a Reply

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

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

Facebook photo

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

Connecting to %s