Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert UTC datetime to another timezone?

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?

like image 279
Jo Smo Avatar asked Aug 09 '12 12:08

Jo Smo


People also ask

How do you convert UTC to timezone?

(GMT-5:00) Eastern Time (US & Canada) Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.

How do I convert datetime to another time zone?

ToLocalTime method. It takes a single parameter, which is the date and time value to convert. You can also convert the time in any designated time zone to local time by using the static ( Shared in Visual Basic) TimeZoneInfo. ConvertTime method.


1 Answers

Use DateTime and DateTimeZone.

$date = new DateTime('2012-07-16 01:00:00 +00');
$date->setTimezone(new DateTimeZone('Europe/Moscow')); // +04

echo $date->format('Y-m-d H:i:s'); // 2012-07-15 05:00:00 
like image 192
Florent Avatar answered Oct 06 '22 00:10

Florent