简体   繁体   中英

Converting timezone offset to friendly name in PHP

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.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM