I am having problem using the new DateTimeZone function. Here is my code:
$userTimezone = new DateTimeZone('America/New_York');
$gmtTimezone = new DateTimeZone('America/Los_Angeles');
$myDateTime = new DateTime('2014-02-19 15:00', $gmtTimezone);
$offset = $userTimezone->getOffset($myDateTime);
echo $offset;
echo date('h:i a', $myDateTime->format('U') + $offset);
It outputs:
-180000
1:00 pm
However, the offset seems to always be off 1 hour. It should be listing 12:00 pm. Any ideas why this may be happening?
QMonkey
2
I don’t think you need to calculate anything like that. You can just set the time zone on your date object and it will convert the time for you.
$userTimezone = new DateTimeZone('America/New_York');
$gmtTimezone = new DateTimeZone('America/Los_Angeles');
$myDateTime = new DateTime('2014-02-19 15:00', $gmtTimezone);
echo $myDateTime->format( 'h:i a' ); // 03:00 pm
$myDateTime->setTimezone( $userTimezone );
echo $myDateTime->format( 'h:i a' ); // 06:00 pm
(note that your variable is called gmt but isn’t. that’s why it is not showing 12:00 pm)
A daylight savings time thing?