How to Convert UTC/GMT Time to Localtime in PHP?

Problem:

You want to convert an UTC/GMT(Coordinated Universal Time/Greenwich Mean Time) time to your local time. It could be the current time or a specific time.

Solution:

In the following you’ll see few ways to convert an UTC/GMT time to your localtime. In the example, it is assumed that you’re in Rome, Italy. You can always choose your desired time zone from the PHP’s list of supported timezone.

Method 1: Using built-in classes to convert current UTC time

We’ll use PHP’s supplied DateTime and DateTImeZone classes to convert current UTC/GMT time to local Rome time.

The following example shows how to do it-

<?php
$UTCObj = new DateTime("now", new DateTimeZone("UTC"));
echo "Current UTC Time is: ". $UTCObj->format("F d, Y g:i a") . "<br />";

$LocalObj = $UTCObj;

$LocalObj->setTimezone(new DateTimeZone("Europe/Rome"));
echo "Current Rome Time is: ". $LocalObj->format("F d, Y g:i a") . "\n";
?>

[wpdm_file id=166]

Output:
Current UTC Time is: July 08, 2014 2:21 pm
Current Rome Time is: July 08, 2014 4:21 pm

How it works:

Line 2 Using DateTImeZone class, we  create an object of UTC time zone. Then, we create a new DateTIme object($UTCObj) which represents the current time(using the first parameter – “now”) according to the UTC time zone.
Line 3 Using the just created $UTCObj object, we print the current time of UTC according to the specified format in the format() method.
Line 5 Here, we create a new copy of $UTCObj object which is $LocalObj. We can use $UTCObj object to manipulate current UTC time related issues and $LocalObj to manipulate current Rome time related tasks.
Line 7 As you know at this point $LocalObj object represents the current UTC time. Now, we’ll use this object to change its time zone to the Rome time. setTimezone() method helps to accomplish this.
Line 8 Here, we just print the current Rome time in the format specified in the format() method.

 

Method 2: Using built-in classes to convert any specific time UTC time

Here, again, we’ll use DateTime and DateTImeZone classes to convert a specific UTC/GMT time to its equivalent local Rome time.

The following example shows how to find out local time of Rome when UTC time is 25th April 2014 8:25 am.

<?php
$UTCObj = DateTime::createFromFormat("j-M-Y g:i", "25-Apr-2014 8:25", new DateTimeZone("UTC"));
echo "UTC Time is: ". $UTCObj->format("F d, Y g:i a") . "<br />";

$LocalObj = $UTCObj;

$LocalObj->setTimezone(new DateTimeZone("Europe/Rome"));
echo "Rome Time is: ". $LocalObj->format("F d, Y g:i a") . "\n";
?>

[wpdm_file id=167]

Output:
UTC Time is: April 25, 2014 8:25 am
Rome Time is: April 25, 2014 10:25 am

How it works:

Line 2 At first, using DateTImeZone class, we  create an object of UTC time zone. Then, we create a new DateTIme object($UTCObj) of the specific time(25th April 2014 8:25 am)  of UTC time zone.
Line 3 Using the created $UTCObj object, we simply print the time again in the specified format in the format() method.
Line 5 Here, we create a new copy of $UTCObj object which is $LocalObj so that we can manipulate UTC time related issues using $UTCObj object and $LocalObj to manipulate Rome time related tasks.
Line 7 As you know, at this point, $LocalObj object represents the specified UTC time. Now, we’ll use this object to change its time zone to the Rome time. setTimezone() method helps to accomplish this.
Line 8 Here, we just print the Rome time in the format specified in the format() method whoch is equivalent to the 25th April 2014 8:25 am UTC time .