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

时间:2021-08-21 19:14:35

For example, I would be able to type a function like so:

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

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

How would such a function be possible?

这样一个函数怎么可能呢?

Also, please note I'm working on a hosted server here, so the server time may be anything.

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

Bonus votes for the smallest possible function.

对最小的函数进行额外的投票。

4 个解决方案

#1


2  

You're going to need a database containing time zone information for every city that is to be queried, and then make a time zone adjustment to your current server time.

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

There is a Time Zone database located here. It looks like it comes with some C code to dump the time zone information to a file that, hopefully, is importable to a database table. Once you have that, you should be able to get a time zone adjustment for any city in the world, at any time of the year.

这里有一个时区数据库。看起来它附带了一些C代码,可以将时区信息转储到一个文件中,希望该文件对数据库表具有重要的意义。一旦你有了这些,你就可以在一年中的任何时候,为世界上任何一个城市调整时区了。

#2


1  

Check this webservice this is helpful to achieve timezone of the city along with local time.

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

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

http://www.earthtools.org/webservices.htm时区

here's it's example (for New York).

这是纽约的例子。

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

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

Note that there are some usage restrictions:

注意,有一些使用限制:

  1. You must not make more than one request per second to these webservices.

    对于这些webservices,您不能超过每秒一个请求。

  2. You must cache results if you believe that you will need to make another identical request within any 24-hour period.

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

  3. You must delete any cached data when you no longer need it and in any case after 14 days.

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

#3


0  

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

Try this.

试试这个。

#4


0  

The date and time functions works according to server, so you have to manually set timezone before any date and time functions are called. And since you are in need of a function that will show the time depending upon the argument passed, the effecient way will be settign the timezone depending upon the argument passed. For that you will require an database or probably an array of timezones (array will be too big with hundreds of indexes) and match them accordingly.

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

Below is an short pseudo code

下面是一个简短的伪代码

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
    }

#1


2  

You're going to need a database containing time zone information for every city that is to be queried, and then make a time zone adjustment to your current server time.

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

There is a Time Zone database located here. It looks like it comes with some C code to dump the time zone information to a file that, hopefully, is importable to a database table. Once you have that, you should be able to get a time zone adjustment for any city in the world, at any time of the year.

这里有一个时区数据库。看起来它附带了一些C代码,可以将时区信息转储到一个文件中,希望该文件对数据库表具有重要的意义。一旦你有了这些,你就可以在一年中的任何时候,为世界上任何一个城市调整时区了。

#2


1  

Check this webservice this is helpful to achieve timezone of the city along with local time.

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

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

http://www.earthtools.org/webservices.htm时区

here's it's example (for New York).

这是纽约的例子。

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

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

Note that there are some usage restrictions:

注意,有一些使用限制:

  1. You must not make more than one request per second to these webservices.

    对于这些webservices,您不能超过每秒一个请求。

  2. You must cache results if you believe that you will need to make another identical request within any 24-hour period.

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

  3. You must delete any cached data when you no longer need it and in any case after 14 days.

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

#3


0  

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

Try this.

试试这个。

#4


0  

The date and time functions works according to server, so you have to manually set timezone before any date and time functions are called. And since you are in need of a function that will show the time depending upon the argument passed, the effecient way will be settign the timezone depending upon the argument passed. For that you will require an database or probably an array of timezones (array will be too big with hundreds of indexes) and match them accordingly.

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

Below is an short pseudo code

下面是一个简短的伪代码

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
    }