How to get Time Zone through IP Address in PHP(如何在 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 地址获取时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 PHP 中通过 IP 地址获取时区
基础教程推荐
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
