42

I am converting this time and date:

Thu, 31 Mar 2011 02:05:59 GMT

To the following time and date format:

Monday March 28 2011 4:48:02 PM

I am using the following PHP code to accomplish this, but I want to convert all time zones to PST/PDT. I looked at the PHP manual and saw this date_default_timezone_set() but I am not sure how to implement that into the code I have below.

$date = $messages[0]->CreationTime;
echo date('l F j Y g:i:s A I', strtotime($date))
FAFAFOHI
  • 909
  • 2
  • 11
  • 15

2 Answers2

98

I would not use date_default_timezone_set for general TZ conversions. (To clarify... if this is for display purposes, script wide, then using the default timezone is a reasonable thing to do.)

Instead, I would use something like:

$tz = new DateTimeZone('America/Los_Angeles');

$date = new DateTime('Thu, 31 Mar 2011 02:05:59 GMT');
$date->setTimezone($tz);
echo $date->format('l F j Y g:i:s A I')."\n";
Kalzem
  • 7,320
  • 6
  • 54
  • 79
Matthew
  • 47,584
  • 11
  • 86
  • 98
2
$date = $messages[0]->CreationTime;
date_default_timezone_set('America/Los_Angeles');
echo date('l F j Y g:i:s A I', strtotime($date));

See this list for available timezones that get passed into the function

Mike B
  • 31,886
  • 13
  • 87
  • 111
  • 1
    Here's a list of the supported arguments into date_default_timezone_set: http://www.php.net/manual/en/timezones.php – Zach Rattner Mar 31 '11 at 16:31
  • 1
    changing your default timezone is a bad idea. you should really do that sort of thing in your php.ini file. if you change the default timezone, other dates could be affected when you arent expecting it. – Brent Larsen Nov 16 '15 at 19:25
  • @BrentLarsen Time zones should be per application not globally on the server. – Mike B Nov 18 '15 at 03:13