Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将时区从名称转换为UTC_Php - Fatal编程技术网

Php 将时区从名称转换为UTC

Php 将时区从名称转换为UTC,php,Php,对不起,我仍然不知道如何为这个问题写一个好标题。 在数据库中,我有一个时区字符串,例如:“亚洲/曼谷”,我想将其转换为“+07:00”。我怎么能做到? 这是我的密码: $newTZ = new DateTimeZone("Asia/Bangkok"); 但我不知道下一步是什么。非常感谢。只需将时区设置为DateTime实例,并使用“p”格式显示即可 只需将时区设置为DateTime实例,并使用“p”格式显示即可 //GMT $time='2018年1月1日上午12:00'; $zone='亚洲

对不起,我仍然不知道如何为这个问题写一个好标题。 在数据库中,我有一个时区字符串,例如:“亚洲/曼谷”,我想将其转换为“+07:00”。我怎么能做到? 这是我的密码:

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

但我不知道下一步是什么。非常感谢。

只需将时区设置为
DateTime
实例,并使用“p”格式显示即可


只需将时区设置为
DateTime
实例,并使用“p”格式显示即可

//GMT
$time='2018年1月1日上午12:00';
$zone='亚洲/曼谷';
$schedule_date=new DateTime($time,new DateTimeZone($zone));
$schedule_date->setTimeZone(新日期时区('UTC'));
$time2=$schedule_date->format('Y-m-d H:i:s');
//转换到时区的时间
echo$time2;
//比较两个日期之间的时间
$date1=创建日期($time);
$date2=创建日期($time2);
回声“;
$object=date_diff($date1,$date2);
//详细对象
打印(对象);
//得到你想要的任何格式。
echo$object->h.“:”$对象->i;
//GMT
$time='2018年1月1日上午12:00';
$zone='亚洲/曼谷';
$schedule_date=new DateTime($time,new DateTimeZone($zone));
$schedule_date->setTimeZone(新日期时区('UTC'));
$time2=$schedule_date->format('Y-m-d H:i:s');
//转换到时区的时间
echo$time2;
//比较两个日期之间的时间
$date1=创建日期($time);
$date2=创建日期($time2);
回声“;
$object=date_diff($date1,$date2);
//详细对象
打印(对象);
//得到你想要的任何格式。
echo$object->h.“:”$对象->i;

哇,这比我的答案更简单、更好+哇,这比我的答案更简单更好+1记住,如果没有时间实例应用分区标识符,则分区标识符毫无意义。记住,如果没有时间实例应用分区标识符,则分区标识符毫无意义
$newTZ = new DateTimeZone("Asia/Bangkok");
echo (new DateTime('now', $newTZ))->format('P'); // displays "+07:00" for 'now'
//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;