See note for the 2nd parameter of DateTime::__construct($time, $timezone)
method:
The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
That means, if you enter $time
in format like yours: 2013-10-28T16:30:55.000Z
, given timezone as 2nd parameter to DateTime constructor will be ignored. See examples where all given timezones are ignored, and timezone from input is used (Z = Zulu = UTC = +00:00).
$dt = new DateTime('2013-10-28T16:30:55.000Z', new DateTimezone('Africa/Dakar'));
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// this parameter is ignored
If you wish to change timezone for given DateTime object, you can use setTimezone()
method, after you created the DateTime object (demo):
$dt = new DateTime('2013-10-28T16:30:55.000Z');
$dt->setTimezone(new DateTimezone('Africa/Dakar'));
Try something like this:
$this->timeStamp = new \DateTime($location->timeStamp);
$local = clone $this->timeStamp;
if ($apiTime instanceof \DateTimeZone) $local->setTimezone($apiTime);
$this->localTimeStamp = $local;
$this->localTimeStampFormatted = $local->format('m/d/Y g:iA T');