如何用PHP查找世界上任何一个城市的当前时间?

时间:2012-11-23 06:37:06

标签: php date time

例如,我可以输入如下函数:

echo findtimeinworld('<some way to format Sydney, Australia>'); // The current time in Sydney, Australia in a timestamp

这样的功能怎么可能?

另外,请注意我在这里使用托管服务器,因此服务器时间可能是任何东西。

奖励最小可能功能。

4 个答案:

答案 0 :(得分:2)

您将需要一个包含要查询的每个城市的时区信息的数据库,然后根据当前服务器时间进行时区调整。

有一个时区数据库located here。看起来它带有一些C代码,用于将时区信息转储到希望可导入数据库表的文件中。一旦你有了这个,你应该能够在一年中的任何时候为世界上任何一个城市进行时区调整。

答案 1 :(得分:1)

查看此网络服务,这有助于实现城市的时区以及当地时间。

http://www.earthtools.org/webservices.htm#timezone

这是它的例子(纽约)。

http://www.earthtools.org/timezone-1.1/40.71417/-74.00639

请注意,有一些使用限制:

  
      
  1. 您不得每秒向这些网络服务发出多个请求。

  2.   
  3. 如果您认为在任何24小时内需要再提出相同的请求,则必须缓存结果。

  4.   
  5. 您不再需要时,必须删除所有缓存数据,并且无论如何都必须在14天后删除。

  6.   

答案 2 :(得分:0)

date_default_timezone_set('Europe/London');
date_default_timezone_get();

试试这个。

答案 3 :(得分:0)

日期和时间功能根据服务器工作,因此您必须在调用任何日期和时间函数之前手动设置时区。 而且由于你需要一个根据传递的参数显示时间的函数,因此有效的方法将根据传递的参数设置时区。 为此,您将需要一个数据库或可能是一个时区阵列(数组太大,有数百个索引)并相应地匹配它们。

下面是一个简短的伪代码

echo findtimeinworld('COUNTRY/CITY'); //findtimeinworld('Australia/Melbourne');

    function findtimeinworld($suppliedTZ){

        //Match $suppliedTZ with timezone database for complete match
        //if match found
        date_default_timezone_set('returned timezone'); 
        //else if match not found
        //match for first part ex Australia and limit the result to 1
        //if match found
        date_default_timezone_set('first returned timezone'); 
        //else if match not found
        date_default_timezone_set('your default timezone');

        //put date and time functions here
    }