Reputation: 18600
I want to convert my local timezone dateTime to UTC timezone DateTime.
date_default_timezone_set('Asia/Calcutta');
$datetime = "2016-05-05 18:33:00";
echo date_default_timezone_get()."<br>"; // Asia/Calcutta
date_default_timezone_set('UTC');
echo date_default_timezone_get()."<br>"; //UTC
echo $utcDateTime = date("Y-m-d H:i:s",strtotime($datetime));
Current Output
2016-05-05 18:33:00
But, Problem is do not convert local dateTime to UTC dateTime. It will give output as Local DateTime.
Upvotes: 2
Views: 7292
Reputation: 7111
Use date helper:
local_to_gmt($time = '');
// $gmt = local_to_gmt(time());
Takes a UNIX timestamp as input and returns it as GMT.
Docs.
Upvotes: 0
Reputation: 35321
You will need to pass the date to strtotime
before you change the timezone with date_default_timezone_set
. Like so:
date_default_timezone_set('Asia/Calcutta');
$datetime = "2016-05-05 18:33:00";
$asia_timestamp = strtotime($datetime);
echo date_default_timezone_get()."<br>"; // Asia/Calcutta
date_default_timezone_set('UTC');
echo date_default_timezone_get()."<br>"; //UTC
echo $utcDateTime = date("Y-m-d H:i:s", $asia_timestamp);
Upvotes: 5
Reputation: 13549
If I've understood it well, it should work:
$date = DateTime::createFromFormat('Y-m-d H:i:s', '2016-05-05 18:33:00');
# you could to pass in timezone too
# $date = DateTime::createFromFormat('Y-m-d H:i:s', '2016-05-05 18:33:00',
# new DateTimeZone('America/Fortaleza'));
var_dump($date->setTimezone(new DateTimeZone('UTC')));
Upvotes: 1