I'm trying to convert my local time to UTC. My code example is:
$s = '2017-06-08T18:00:00.000Z';
$d = new \DateTime($s, new \DateTimeZone('Asia/Vladivostok'));
print_r($d->getTimeZone());
$d = $d->setTimeZone(new \DateTimeZone('UTC'));
print_r($d->getTimeZone());
echo $d->format('Y-m-d H:i:s') . "
";
But I have invalid timezone on just created date time and converting return me same time.
Output:
DateTimeZone Object
(
[timezone_type] => 2
[timezone] => Z
)
DateTimeZone Object
(
[timezone_type] => 3
[timezone] => UTC
)
2017-06-08 18:00:00
I can set timezone after creating
$s = '2017-06-08T18:00:00.000Z';
$d = new \DateTime($s, new \DateTimeZone('Asia/Vladivostok'));
// Added
$d->setTimeZone(new \DateTimeZone('Asia/Vladivostok'));
print_r($d->getTimeZone());
$d = $d->setTimeZone(new \DateTimeZone('UTC'));
print_r($d->getTimeZone());
echo $d->format('Y-m-d H:i:s') . "
";
But it not helps, I have correct timezone but time is same.
DateTimeZone Object
(
[timezone_type] => 3
[timezone] => Asia/Vladivostok
)
DateTimeZone Object
(
[timezone_type] => 3
[timezone] => UTC
)
2017-06-08 18:00:00
You can change time zone with a DateTimeZone
object. Here's an example of this:
// INIT TIMEZONE OBJECTS
$utcTime = new DateTimeZone("UTC");
$yourTime = new DateTimeZone("Asia/Vladivostok");
// CREATE DATA WITH YOUR TIMEZONE
$date = new DateTime("2017-01-01 15:00:00", $yourTime);
// ADJUST TIMEZONE TO UTC
$date->setTimezone( $utcTime );
// PRINT
echo $date->format('Y-m-d H:i:s');
You can achieve this by using date_default_timezone_set() and date_default_timezone_get() functions.
$date = strtotime("2017-06-15 00:00:00");
echo date_default_timezone_get() . "<br />";
echo date("Y-d-mTG:i:sz",$date) . "<br />";
echo date_default_timezone_set("UTC") . "<br />";
echo date("Y-d-mTG:i:sz", $date) . "<br />";