In many, we need to get user address on the basis of user profile address or IP. We will tell you how to get user timezone on the basis of user address. If you want to calculate it by Ip address then you have to add one more step. You have to get user address first by his Ip address then follow the below instructions.
To get timezone of the user based on user profile address, first, we will call google map API to get user longitude and latitude. When we have user longitude and latitude then we will call google API to get timezone of the user. Then we can save the timezone of the user in the database so that we need not to make API call again and again. Please use below code to get timezone of the user based on profile address.
$country = (isset($country))?str_replace(' ', '', $country):'';
$state = (isset($country))?str_replace(' ', '', $state):'';
$city = (isset($country))?str_replace(' ', '', $city):'';
$geocode_stats = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=$city+$state+$country,&sensor=false");
$output_deals = json_decode($geocode_stats);
$latLng = $output_deals->results->geometry->location;
$lat = $latLng->lat;
$lng = $latLng->lng;
$google_time = file_get_contents("https://maps.googleapis.com/maps/api/timezone/json?location=$lat,$lng×tamp=1331161200&key=AIzaSyAGwPLv0zlwcI2a0rnsP5t-7xIVoFMgXmY");
$timez = json_decode($google_time);
If you face any issue in APi then please comment below.