How to get Time Zone through IP Address in PHP [duplicate]

$ip = "189.240.194.147";  //$_SERVER['REMOTE_ADDR']
$ipInfo = file_get_contents('http://ip-api.com/json/' . $ip);
$ipInfo = json_decode($ipInfo);
$timezone = $ipInfo->timezone;
date_default_timezone_set($timezone);
echo date_default_timezone_get();
echo date('Y/m/d H:i:s');

Sometime it won’t work on local server so try on server.

Edit: This data is coming from ip-api.com, they’re free to use as long as you don’t exceed 45 requests per minute and not using commercially. See their TOS, not a long a page.

Leave a Comment