tags:

views:

72

answers:

1

Hello,

The variable below formats as "2009-11-05 22:51:26" when it is printed. How could I format it to "22:51 New York Time 5 Nov 2009 Thursday"? (I think the timestamp in my database is United States eastern time zone).

   createddatetime TIMESTAMP DEFAULT CURRENT_TIMESTAMP

Thanks in advance,

John

+3  A: 
date_default_timezone_set('America/New_York');
$timestamp = "2009-11-05 22:51:26";
echo date('H:i e j M Y l', strtotime($timestamp)); //22:51 America/New_York 5 Nov 2009 Thursday

See date() for a list of format parameters. I matched your requested output pretty closely but you may want to tweak it

strtotime() - Parse about any English textual datetime description into a Unix timestamp

Mike B
Will this work for variable timestamps? It looks like the variable $timestamp you suggest is static.
John
yes, it will. I'm sure Mike B is just doing that as an example.
GSto
What do you mean variable timestamps? Like I said in my answer, strtotime() parses just about any datetime format.
Mike B
That will not convert the timezone properly. strtotime($timestamp) needs to be called *before* date_default_timezone_set(), otherwise it will interpret the timestamp as the new default timezone and not the timezone on the server. TIMESTAMP values are converted to the server's timezone on retrieval. Also note, date_default_timezone_set() only works in PHP >= 5.1.0.
Jason
@Jason Good point. However, as John stated in his question, the datetime string has already been retrieved from the database and is in US/East. I assumed this was true in my response. If this is not the case then my answer will need to be tweaked.
Mike B