65

How do I convert a date/time string (e.g. 2011-01-01 15:00:00) that is UTC to any given timezone php supports, such as America/New_York, or Europe/San_Marino.

jww
  • 97,681
  • 90
  • 411
  • 885
amax
  • 653
  • 1
  • 5
  • 4

6 Answers6

158

PHP's DateTime object is pretty flexible.

$UTC = new DateTimeZone("UTC");
$newTZ = new DateTimeZone("America/New_York");
$date = new DateTime( "2011-01-01 15:00:00", $UTC );
$date->setTimezone( $newTZ );
echo $date->format('Y-m-d H:i:s');
Kevin Peno
  • 9,107
  • 1
  • 33
  • 56
  • This is it. You did it! – Mike Aron Oct 02 '17 at 18:17
  • 1
    Its so flexible it even takes `$text = '2019-02-15T17:10:46+05:30'; $dateTime = new DateTime($date); echo $dateTime->format("M d, Y");` – Sorter May 16 '19 at 22:31
  • @Sorter yes, you can return a date like that and format it, but this question was specifically about converting timezones. Since your datetime string already includes the TZ in it (OPs did not), you can skip the part where I set `$UTC` on the `new DateTime` call and add `$UTC` to the `setTimezone` call instead, transforming your TZ into UTC from `+5:30`! – Kevin Peno May 16 '19 at 22:52
  • 1
    I've been battling this for a day and a half! This was what I needed! – Kelso Sep 19 '19 at 12:50
5

PHP's DateTime object is pretty flexible.

Since the user asked for more than one timezone option, then you can make it generic.

Generic Function

function convertDateFromTimezone($date,$timezone,$timezone_to,$format){
 $date = new DateTime($date,new DateTimeZone($timezone));
 $date->setTimezone( new DateTimeZone($timezone_to) );
 return $date->format($format);
}

Usage:

echo  convertDateFromTimezone('2011-04-21 13:14','UTC','America/New_York','Y-m-d H:i:s');

Output:

2011-04-21 09:14:00

Miguel
  • 3,349
  • 2
  • 32
  • 28
1
function _settimezone($time,$defaultzone,$newzone)
{
$date = new DateTime($time, new DateTimeZone($defaultzone));
$date->setTimezone(new DateTimeZone($newzone));
$result=$date->format('Y-m-d H:i:s');
return $result;
}

$defaultzone="UTC";
$newzone="America/New_York";
$time="2011-01-01 15:00:00";
$newtime=_settimezone($time,$defaultzone,$newzone);
1

Assuming the UTC is not included in the string then:

date_default_timezone_set('America/New_York');
$datestring = '2011-01-01 15:00:00';  //Pulled in from somewhere
$date = date('Y-m-d H:i:s T',strtotime($datestring . ' UTC'));
echo $date;  //Should get '2011-01-01 10:00:00 EST' or something like that

Or you could use the DateTime object.

Phoenix
  • 4,488
  • 1
  • 21
  • 13
0

General purpose normalisation function to format any timestamp from any timezone to other. Very useful for storing datetimestamps of users from different timezones in a relational database. For database comparisons store timestamp as UTC and use with gmdate('Y-m-d H:i:s')

/**
 * Convert Datetime from any given olsonzone to other.
 * @return datetime in user specified format
 */

function datetimeconv($datetime, $from, $to)
{
    try {
        if ($from['localeFormat'] != 'Y-m-d H:i:s') {
            $datetime = DateTime::createFromFormat($from['localeFormat'], $datetime)->format('Y-m-d H:i:s');
        }
        $datetime = new DateTime($datetime, new DateTimeZone($from['olsonZone']));
        $datetime->setTimeZone(new DateTimeZone($to['olsonZone']));
        return $datetime->format($to['localeFormat']);
    } catch (\Exception $e) {
        return null;
    }
}

Usage:

$from = ['localeFormat' => "d/m/Y H:i A", 'olsonZone' => 'Asia/Calcutta'];

$to = ['localeFormat' => "Y-m-d H:i:s", 'olsonZone' => 'UTC'];

datetimeconv("14/05/1986 10:45 PM", $from, $to); // returns "1986-05-14 17:15:00"
Community
  • 1
  • 1
Sandeep
  • 28,307
  • 3
  • 32
  • 24
-2

How about:

$timezone = new DateTimeZone('UTC');
$date = new DateTime('2011-04-21 13:14', $timezone);
echo $date->format;
Adnan
  • 25,882
  • 18
  • 81
  • 110
  • `$date->format()` requires 1 parameter. See [this](http://php.net/manual/en/datetime.format.php) – Raptor Jun 25 '15 at 06:13
  • $date = new DateTime('2011-04-21 13:14', $timezone) Isn't this missing the seconds for UTC? – Quadrivium Oct 06 '15 at 21:50
  • Unless I'm missing something, this code (even if fixed to use `format` as method) doesn't convert between time zones as requested: `$date` will be at UTC. – Álvaro González Mar 28 '16 at 10:56