php 如何按国家/地区名称查找 GMT 日期/时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3483519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find GMT date/time by country name?
提问by Artefacto
How can I find country name -> GMT date/time to that I can do like following:
如何找到国家/地区名称 -> GMT 日期/时间,我可以执行以下操作:
Example:
例子:
$datetime = new GMT_search('America'); //output: 2010-01-01 00:00:00
$datetime = new GMT_search('India'); //output: 2010-01-01 ??:??:??
$datetime = new GMT_search('China'); //output: 2010-01-01 ??:??:??
I tried gmdate()
, date_default_timezone_set('Asia/....');
, and ini_set('date.timezone','China');
but it's not exactly helping me to find easily country name to GMT date/time.
我试过gmdate()
, date_default_timezone_set('Asia/....');
,ini_set('date.timezone','China');
但这并不能完全帮助我轻松找到 GMT 日期/时间的国家/地区名称。
Can anyone please kindly show me a PHP example, which really works?
任何人都可以请给我看一个真正有效的 PHP 示例吗?
Thank you
谢谢
回答by Artefacto
You can search the timezones by country with DateTimeZone::listIdentifiers
.
您可以使用 来按国家/地区搜索时区DateTimeZone::listIdentifiers
。
Example, to get the timezones in Portugal:
例如,要获取葡萄牙的时区:
print_r(DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, "PT"));
gives:
给出:
Array ( [0] => Atlantic/Azores [1] => Atlantic/Madeira [2] => Europe/Lisbon )
You can then do:
然后你可以这样做:
$d = new DateTime("now", new DateTimeZone("Atlantic/Azores"));
echo $d->format(DateTime::W3C); //2010-08-14T15:22:22+00:00
As has been repeated over and over again in this thread, you can't get one single time zone per country. Countries have several timezones, and you'll notice that even this pagedoesn't even select one arbitrarily for some countries like the U.S.A.
正如在此线程中一遍又一遍重复的那样,您无法为每个国家/地区设置一个时区。国家/地区有多个时区,您会注意到,即使此页面甚至没有为美国等某些国家/地区任意选择一个时区
回答by DrColossos
Not sure if this is what you are looking for but PEARhas a "Date" package available that has a nice example that seems to do what you are asking for.
不确定这是否是您要找的,但PEAR有一个“ Date”包可用,它有一个很好的例子,似乎可以满足您的要求。
http://pear.php.net/manual/en/package.datetime.date.examples.php(scroll down to "Converting timezones")
http://pear.php.net/manual/en/package.datetime.date.examples.php(向下滚动到“转换时区”)
回答by Jungle Hunter
Hereyou'll find the complete list of timezones supported by PHP, which are meant to be used with e.g. date_default_timezone_set()
. Support for countries with multiple timezones is also convenient to look up. Take the example of the American region.
在这里,您将找到 PHP 支持的完整时区列表,这些时区旨在与例如date_default_timezone_set()
. 对具有多个时区的国家的支持也很方便查找。以美洲地区为例。
<?php
date_default_timezone_set('America/New_York');
echo date('D,F j, Y, h:i:s A');
?>
The list is a complete timezones supported by PHP, which are meant to be used with e.g. date_default_timezone_set().
该列表是 PHP 支持的完整时区,旨在与 date_default_timezone_set() 等一起使用。
回答by geon
You can't do it quite like that, since a lot of countries have multiple timezones. You could store the timezone by the name of a city instead, but I'd use an integer with the timezone offset in seconds.
你不能那样做,因为很多国家都有多个时区。您可以改为按城市名称存储时区,但我会使用一个整数,时区偏移量以秒为单位。
You can get a list of timezones by continent/city from this function: [www.php.net/manual/en/function.timezone-identifiers-list.php][1]
你可以从这个函数中获取大陆/城市的时区列表:[www.php.net/manual/en/function.timezone-identifiers-list.php][1]
You can get the respective offset from this function: [www.php.net/manual/en/datetimezone.getoffset.php][2]
您可以从此函数中获取相应的偏移量:[www.php.net/manual/en/datetimezone.getoffset.php][2]
When you have a valid offset, you can use gmdate() instead of date() to get a date in your format without the timezone/dst adjustment. Just pass the time() + the ajustment you have stored: [www.php.net/manual/en/function.gmdate.php][3]
当您有一个有效的偏移量时,您可以使用 gmdate() 而不是 date() 以您的格式获取日期而无需时区/dst 调整。只需通过 time() + 您存储的调整:[www.php.net/manual/en/function.gmdate.php][3]