This article shows how you can get TimeZone name and id in PHP from latitude and longitude value by using Google Maps Time Zone API.
Note that: You need to put your API Key in the maps api url like this:
https://maps.googleapis.com/maps/api/timezone/json?location=$latitude,$longitude×tamp=$time&key=xxxxxxxxxxxxxxxxxxxxxYou can get the API key from here: Get API Key
Here is the source code:
$latitude = 27.7172453;
$longitude = 85.3239605;
$time = time();
$url = "https://maps.googleapis.com/maps/api/timezone/json?location=$latitude,$longitude×tamp=$time&key=YOUR-API-KEY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$responseJson = curl_exec($ch);
curl_close($ch);
$response = json_decode($responseJson);
//var_dump($response);
echo $response->timeZoneId;
Get time from timezone in PHP
$timezone = $response->timeZoneId; // TIMEZONE VALUE YOU GET FROM THE RESPONSE OF MAPS API
$date = new DateTime("now", new DateTimeZone($timezone));
echo $date->format('Y-m-d H:i:s');
Get time difference between the timezone time and GMT time
$timezone = $response->timeZoneId; // TIMEZONE VALUE YOU GET FROM THE RESPONSE OF MAPS API
$date = new DateTime("now", new DateTimeZone($timezone));
$date_1 = $date->format('Y-m-d H:i:s'); // DATE TIME OF THE FETCHED TIMEZONE
$date_2 = gmdate('Y-m-d H:i:s'); // DATE TIME OF GMT
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%h %i'); // TIME DIFFERENCE IN HOURS & SECONDS
Another way to get time difference between two timezones
$origin_tz = $response->timeZoneId; // TIMEZONE VALUE YOU GET FROM THE RESPONSE OF MAPS API
$remote_tz = 'UTC'; // UTC TIMEZONE
$origin_dtz = new DateTimeZone($origin_tz);
$remote_dtz = new DateTimeZone($remote_tz);
$origin_dt = new DateTime("now", $origin_dtz);
$remote_dt = new DateTime("now", $remote_dtz);
$offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt); // TIME DIFFERENCE IN SECONDS
echo $offset/ (60*60); // TIME DIFFERENCE IN HOURS
Hope this helps. Thanks.