views:

373

answers:

3

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.

A: 

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

Sarfraz
Using date() + strtotime() doesn't seem to do the trick because it converts it to the server's timezone (in my case PST).
Ian Silber
@Silber: see my answer updated.
Sarfraz
+2  A: 

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

Mike B
Using date() + strtotime() doesn't seem to do the trick because it converts it to the server's timezone (in my case PST).
Ian Silber
See my update about `date_parse_from_format()`
Mike B
A: 

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.

Evert