如何在 PHP 中通过 IP 地址获取时区 [英] How to get Time Zone through IP Address in PHP

查看:19
本文介绍了如何在 PHP 中通过 IP 地址获取时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 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.

这篇关于如何在 PHP 中通过 IP 地址获取时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆