Jaylen Jaylen - 1 year ago 85
PHP Question

How to convert between time zones in PHP using the DateTime class?

I am trying to convert time between current time to UTC and UTC to current time zone.

Here is what I have done:

$schedule_date = new DateTime($triggerOn, new DateTimeZone('UTC') );
$triggerOn = $schedule_date->format('Y-m-d H:i:s');

echo $triggerOn;

The output value does not change the only thing that changes in format.

the string
was generated based on

This is how my string looks like before and after:

BEFORE 04/01/2013 03:08 PM
AFTER 2013-04-01 15:08:00

So the issue here is that DateTime does not convert to UTC.

Answer Source

What you're looking for is this:

$triggerOn = '04/01/2013 03:08 PM';
$user_tz = 'America/Los_Angeles';

echo $triggerOn; // echoes 04/01/2013 03:08 PM

$schedule_date = new DateTime($triggerOn, new DateTimeZone($user_tz) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$triggerOn =  $schedule_date->format('Y-m-d H:i:s');

echo $triggerOn; // echoes 2013-04-01 22:08:00
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download