Gao Lin
Gao Lin

Reputation: 59

PHP Timezone not working correctly

So we have in-house servers here in this EDT part of the USA America/New_York and for someone reason, this [SSCCE][1] code is not working.

<?php
echo date_default_timezone_get() . " <br />";

$dateCal = "2015-06-22 13:00:00"; // EDT Time
$schedule_date = new DateTime($dateCal, new DateTimeZone("America/Chicago") );

echo $dateCal  . " <br />"; // 2015-06-22 13:00:00
echo date_format($schedule_date, 'Y-m-d H:i:s T'); // 2015-06-22 13:00:00 CDT
?>

The output results in:

UTC 
2015-06-22 13:00:00 
2015-06-22 13:00:00 CDT

When the 2nd time should be 12 NOON, instead of 1PM.

How do I fix this?

Upvotes: 0

Views: 85

Answers (1)

John Conde
John Conde

Reputation: 219894

You're not quite handling timezones correctly

// set the datetime in the EDT timezone
$schedule_date = new DateTime("2015-06-22 13:00:00", new DateTimeZone("America/New_York") );
echo $schedule_date->format('Y-m-d H:i:s T'). " <br />"; // 2015-06-22 13:00:00
// change it to CDT
$schedule_date->setTimeZone(new DateTimeZone('America/Chicago'));
echo $schedule_date->format('Y-m-d H:i:s T'). " <br />"; // 2015-06-22 12:00:00

Demo

Upvotes: 4

Related Questions