UTC Date / Time String for Time Zone

How can I convert a date / time string (e.g. 2011-01-01 15:00:00), which is UTC for any supported php timezone zones, such as America / New_York or Europe / San_Marino.

+48
timezone php utc
Apr 21 '11 at 15:48
source share
6 answers

The PHP DateTime object is quite 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('Ymd H:i:s'); 
+114
Apr 21 2018-11-11T00:
source share

The PHP DateTime object is quite flexible.

Since the user has requested more than one time zone parameter, you can make it general.

General 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); } 

Application:

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

Output:

2011-04-21 09:14:00

+2
01 Oct '15 at 10:08
source share

Assuming UTC is not included in the string, follow these steps:

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

Or you can use a DateTime object.

+1
Apr 21 '11 at 15:59
source share
 function _settimezone($time,$defaultzone,$newzone) { $date = new DateTime($time, new DateTimeZone($defaultzone)); $date->setTimezone(new DateTimeZone($newzone)); $result=$date->format('Ymd H:i:s'); return $result; } $defaultzone="UTC"; $newzone="America/New_York"; $time="2011-01-01 15:00:00"; $newtime=_settimezone($time,$defaultzone,$newzone); 
+1
Aug 14 '15 at 3:54 on
source share

General purpose normalization function to format any timestamp from any time zone to another. Very useful for storing datetimestamps of users from different time zones in a relational database. To compare databases, mark the timestamp as UTC and use with gmdate('Ymd 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'] != 'Ymd H:i:s') { $datetime = DateTime::createFromFormat($from['localeFormat'], $datetime)->format('Ymd 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; } } 

Application:

 $from = ['localeFormat' => "d/m/YH:i A", 'olsonZone' => 'Asia/Calcutta']; $to = ['localeFormat' => "Ymd H:i:s", 'olsonZone' => 'UTC']; datetimeconv("14/05/1986 10:45 PM", $from, $to); // returns "1986-05-14 17:15:00" 
0
Dec 09 '16 at 6:04
source share

What about:

 $timezone = new DateTimeZone('UTC'); $date = new DateTime('2011-04-21 13:14', $timezone); echo $date->format; 
-2
Apr 21 '11 at 15:53
source share



All Articles