通过他们的lat / long / city在php中获取用户当前的时间 [英] Get users current time in php with their lat/long/city

查看:190
本文介绍了通过他们的lat / long / city在php中获取用户当前的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 http://ipinfodb.com/ API来获取用户lat / long .. 。
我拥有数组格式的用户时区。数组返回以下信息...

  array(11){
[statusCode] =>字符串(2)确定...
[countryCode] =>字符串(2)CA
[countryName] =>字符串(6)CANADA
[regionName] =>字符串(16)BRITISH COLUMBIA
[cityName] =>字符串(8)VANCOUVER....
[timeZone] =>字符串(6)-07:00
}

,我如何找到用户目前的时间,当我有他们的时区格式为-07:00,04:00等等等等

解决方案

时区不是抵消!令我困扰的是,如此多的API会造成这种常见的错误。不要被被返回字段称为时区的事实所愚弄。



时区是一个区域,它可以在整个区域中有一个或多个偏移量年。您可以解析时区和日期的偏移量,但无法走向另一个方向! TZDB时区列表在这里



具体时区为美国/温哥华 - 不是因为您的经纬度与温哥华市相匹配,而是因为温哥华恰好是您的位置的时区。不是每个城市都有自己的时区ID,所以你不能做出任何假设。


$ b -07:00是当前偏移量 America / Vancouver 时区。恰好是夏令时抵消。当夏令时结束时,它将变为 -08:00 。看起来您使用的特定Web服务不会显示此信息。

有关StackOverflow的其他几个问题已经回答了如何从实际时区解析实际时区经纬度。您可以调用Web服务,也可以使用自己的数据库。以下是一些链接:





我相信还有更多。我试图把它作为一个副本来关闭,但我之前没有看到ipinfodb.com。



如果您确定您的API返回了当前偏移量,并且您只需要用户的当前时间,那么Jonathon在问题评论中指出,您可以将偏移量应用于当前UTC时间,然后您就会知道时间在那个地方。但是你必须每次都这样做。您不能保存该偏移量并稍后再使用它。为此,您需要全时区参考。


I'm using http://ipinfodb.com/ api in order to get users lat/long... I have users timezone in the format of an array. The array returns the following information...

array(11) { 
     ["statusCode"]=> string(2) "OK"... 
     ["countryCode"]=> string(2) "CA" 
     ["countryName"]=> string(6) "CANADA" 
     ["regionName"]=> string(16) "BRITISH COLUMBIA" 
     ["cityName"]=> string(8) "VANCOUVER" ....
     ["timeZone"]=> string(6) "-07:00" 
}

With the information from the array, how would i find the users current time when i have their timezone in the format of -07:00, 04:00... etc etc

解决方案

A "time zone" is NOT an offset! It bothers me that so many APIs make this common mistake. Don't be fooled by the fact that the return field is called "timeZone".

A "time zone" is an region that can have one or more offsets throughout the year. You can resolve an offset from a time zone and a date, but you cannot go the other direction! A list of TZDB time zones is here.

The specific time zone would be America/Vancouver - not because your lat/lon matched the city of Vancouver, but because Vancouver happens to be the reference city for the time zone of your location. Not every city has its own time zone id, so you can't make any assumptions.

-07:00 is the current offset for the America/Vancouver time zone. It happens to be the daylight savings offset. When daylight savings ends, it will change to -08:00. It would appear that the particular web service you are using does not reveal this information.

There are several other questions on StackOverflow that already answer how to resolve an actual time zone from a lat/lon. There are web services you can call, or you can use your own database. Here are some links:

I'm sure there are more. I was tempted to close this as a duplicate, but I hadn't seen ipinfodb.com referenced before.

If you are certain that your API returns the current offset, and you just want the user's current time, then as Jonathon pointed out in the question comments, you can just apply the offset to the current UTC time and you'll know what the time is in that location. But you'll have to do this every time. You can't save that offset and use it again later. For that, you need the full time zone reference.

这篇关于通过他们的lat / long / city在php中获取用户当前的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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