如何计算正确的偏移量?

Beginning to learn about the php dateTime & dateTimezone objects. Assuming a server in US/Eastern and creating a time in another timezone, the goal is to convert and store the time (ie London) on the server (US/Eastern), in server time. Using this code:

$runTime = '2013-04-10 07:45:00';
$serverTimezone = new DateTimeZone ( 'US/Eastern');
$checkinTimezone = new DateTimeZone ( 'Europe/London');
$checkinTime = new DateTime ( $runTime, $serverTimezone );

$offset = $serverTimezone -> getOffset ( $checkinTime );

echo "<br/><pre>";
echo 'London: ' . date ( 'Y-m-d H:i', $checkinTime -> format ( 'U' ) ) . "<br/>";
echo "<br/>offset: " . $offset . "<br/>";
echo 'Local: ' . date ( 'Y-m-d H:i', $checkinTime -> format ( 'U' ) + $offset );

-14400 is always returned, wether before or after today (Europe switched to DST earlier today). In an aha moment, I discovered the documentation for getOffset says Returns the timezone offset from GMT.

Is there a simple way to convert that time to US/Eastern? Do I have to go into the getTransitions, find the day DST begins and do the calculation manually? This only matters on the first day of each spring/fall switch.

$fromZone = new DateTimeZone('US/Eastern');
$toZone = new DateTimeZone('UTC'); // assuming the server is going to be UTC going forwards

$dt = new DateTime('2013-04-02 10:00:00', $fromZone);
$dt->setTimezone($toZone);
echo $dt->format('Y-m-d H:i:s');

Outputs: 2013-04-02 14:00:00.

Switch the timezones as needed.


If you're dealing with just the timestamps, you can simplify things somewhat:

$fromZone = new DateTimeZone('US/Eastern');
$dt = new DateTime('2013-04-02 10:00:00', $fromZone);
echo $dt->getTimestamp();

Outputs: 1364911200