Converting timezone offset to friendly name in PHP

瘦欲@ 提交于 2019-12-11 03:51:18

问题


I'm trying to convert a RFC timestamp to a friendly date using PHP. Here's the example:

Wed, 17 Feb 2010 19:44:01 -0500

I'd like this to print as:

Wed, 17 Feb 2010 19:44:01 EST

Using date() + strtotime() doesn't seem to do the trick because it converts it to the server's timezone (in my case PST).

Is there a simple way to do this for all GMT offsets? Seems like there must be.


回答1:


see strototime() and date()

echo date('D, j M Y G:i:s T', strtotime('Wed, 17 Feb 2010 19:44:01 -0500));
//Wed, 17 Feb 2010 19:44:01 EST

Update: To set the timezone see date_timezone_set(). If you need to set the timezone according to the offset in the string you may have to do some parsing. For some help, see date_parse_from_format() and related functions getdate(), date_parse(), etc. http://us2.php.net/manual/en/ref.datetime.php




回答2:


Use the date function along with strtotime eg something like this:

 print date("format-here", strtotime("Wed, 17 Feb 2010 19:44:01 -0500"));

Note: The strtotime function needs proper/acceptable date string.

If you need to convert to local time, see these links:

http://www.askdavetaylor.com/how_do_i_get_local_time_with_the_php_time_function.html

http://php.net/manual/en/function.localtime.php




回答3:


The problem with this is that, while EST equals UTC - 5 hours, -0500 can also mean other timezones such as CDT, which is the Central timezone during daylight savings time.

So simply put, there is no one-to-one mapping. Besides the timezone offset, you would also need to know location in order to determine the friendly name. This is why operating systems ask you for your city when setting up the timezone.



来源:https://stackoverflow.com/questions/2286281/converting-timezone-offset-to-friendly-name-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!