How to convert UTC datetime to another timezone?

Viewed 33522

How can i convert a date like this: 2012-07-16 01:00:00 +00 (it's in the UTC +00:00 timezone) to UTC +04:00 timezone? Ensuring that daylight saving will be handelled correctly?

4 Answers

The DateTimeZone constructor can explicitly accept a UTC offset, which I understand you have.

//So For 'Asia/Kolkata' you need to use 

$timeZone = new DateTimeZone('+0530');

// Example 

$utc = new DateTime("1960-08-08T20:40:00Z");
echo $utc->format('Y-m-d H:i:s T')."<br>";
$tz =new DateTimeZone('+0530');
$utc->setTimezone($tz);
echo $utc->format('Y-m-d H:i:s T');

Output: 1960-08-08 20:40:00 Z 1960-08-09 02:10:00 GMT+0530

I guess this is easiest and simplest way to convert time to any given Offset

Documentation:

http://php.net/manual/en/datetimezone.construct.php

Note: per that documentation link, this new constructor usage has been available since PHP version 5.5.10.

Related