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



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

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

这样的功能怎么可能?

另外,请注意,我正在这里的托管服务器上工作,因此服务器时间可能是任何时间。

奖金投票用于尽可能小的功能。

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

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

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

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

这是它的例子(纽约)。

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

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

  1. 您每秒不得向这些 Web 服务发出超过一个请求。

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

  3. 当您不再需要任何缓存数据时,您必须将其删除,并且在任何情况下都必须在 14 天后删除。

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

试试这个。

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

下面是一个简短的伪代码

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
    }

最新更新