如何在 PHP 中通过 IP 地址获取时区

2021-12-23 00:00:00 time ip timezone php

我想通过 PHP 中的 IP 地址获取时区.实际上,我有一个将在客户端计算机上运行的应用程序.我有客户端机器的 IP 地址.但我无法获得每台客户端机器的时区.

I want to get time zone through an IP Address in PHP. Actually, I have an application which will run at the client machine. I have the IP address of the client machine. But I am not able to get the time zone for each client machine.

推荐答案

$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.

此数据来自 ip-api.com,只要您不超过 每分钟 45 个请求 和 <强>未用于商业用途.查看他们的TOS,不是很长的一页.

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.

相关文章