When using the PHP class DateTime and try setting DateTimeZone , I get different results depending on how I installed it: using DateTime::__construct or using the DateTime::setTimezone .
here is an example:
$date = '2014-08-01' $dateTimeOne = new DateTime($date, new DateTimeZone('America/Los_Angeles')); echo $dateTimeOne->format('Ymd\TH:i:sP'); // 2014-08-01T00:00:00-07:00 $dateTimeTwo = new DateTime($date); $dateTimeTwo->setTimezone(new DateTimeZone('America/Los_Angeles')); echo $dateTimeTwo->format('Ymd\TH:i:sP'); // 2014-07-31T17:00:00-07:00
See also http://3v4l.org/LrZfM
I looked around and did not find an adequate explanation regarding this particular behavior, except for the following comment in php docs: datetime.settimezone and php | architect Date and Time Programming Guide: Working with Time Zones - DateTimeZone .
The comment says that the DateTime::setTimezone will change the time zone for a specific point in time (timestamp), but the Unix timestamp will remain unchanged.
On the other hand, the DateTime::__construct DateTimeZone parameter is used to "overwrite the current default time zone with a user-defined" Chapter 3: Working with time zones - DateTimeZone ,
Other than that, this is not enough (what I could find).
This is what I would like to know:
- Further explanation of these two methods for setting time zones
- When should I use
DateTime::__construct to set the time zone - When should I use
DateTime::setTimezone to set the time zone - Clear example of using one vs another or how to use them together
source share