KaZaT
KaZaT

Reputation: 364

Convert timezone from name to UTC

Sorry but I still don't know how to write a good title for this question. In database, I have a string of timezone, ex: "Asia/Bangkok" and I want to convert it to "+07:00". How I can do it? Here is my code:

$newTZ = new DateTimeZone("Asia/Bangkok");

But I don't know what is next. Thanks you so much.

Upvotes: 0

Views: 58

Answers (2)

Thamaraiselvam
Thamaraiselvam

Reputation: 7080

//GMT
$time = '01/01/2018 12:00 AM';
$zone = 'Asia/Bangkok';

$schedule_date = new DateTime($time, new DateTimeZone($zone) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$time2 =  $schedule_date->format('Y-m-d H:i:s');

//Time conveted to TimeZone
echo $time2;

//Compare time between two dates
$date1=date_create($time);
$date2=date_create($time2);

echo "<pre>";
$object = date_diff($date1, $date2);

//Detailed object
print_r($object);


//Get whatever format you want.
echo $object->h  . ':' . $object->i;

Upvotes: 0

Phil
Phil

Reputation: 165059

Simply set the timezone to a DateTime instance and display using the "P" format

$newTZ = new DateTimeZone("Asia/Bangkok");
echo (new DateTime('now', $newTZ))->format('P'); // displays "+07:00" for 'now'

https://3v4l.org/0Cb0C

Upvotes: 4

Related Questions