使用UTC和php

使用UTC和php,php,time,utc,Php,Time,Utc,我正在使用Live Connect创建日历事件。根据他们的文件,活动的开始时间应表明从UTC(即+0700或-0300)开始的时间有多少小时。首先,我从php手册中拼凑出一些代码,这些代码可以正常工作。然而,它“感觉”相当冗长。那么,从文体的角度来看,有没有一种方法可以把我的内容整理得更简洁?请注意,$timeu_zone是我根据给定用户知道的 $dateTimeZone = new DateTimeZone($time_zone); $dateTime= new DateTime("now",

我正在使用Live Connect创建日历事件。根据他们的文件,活动的开始时间应表明从UTC(即+0700或-0300)开始的时间有多少小时。首先,我从php手册中拼凑出一些代码,这些代码可以正常工作。然而,它“感觉”相当冗长。那么,从文体的角度来看,有没有一种方法可以把我的内容整理得更简洁?请注意,$timeu_zone是我根据给定用户知道的

$dateTimeZone = new DateTimeZone($time_zone);
$dateTime= new DateTime("now", $dateTimeZone);
$gmt_offset = ($dateTime->getOffset())/3600;
$negative = ($gmt_offset<0);
$gmt_offset = abs($gmt_offset);
if ($gmt_offset < 10) {
$gmt_offset = '0'.$gmt_offset.'00';
} else {
    $gmt_offset = $gmt_offset.'00';
}
if ($negative) {
    $gmt_offset = '-'.$gmt_offset;  
} else {
$gmt_offset = '+'.$gmt_offset;
}
$dateTimeZone=新的dateTimeZone($timezone);
$dateTime=新日期时间(“现在”,$DATETIMEONE);
$gmt_offset=($dateTime->getOffset())/3600;
$负=($gmt\u偏移量
从:

格式化字符:O
说明:与格林威治时间(GMT)的差异(以小时为单位)
示例返回值:示例:+0200


我不完全确定你想在这里做什么,但我觉得涉及的数学太多了,DateTime类可以帮你处理。例如,我住在太平洋西北部,所以我的$gmt_偏移量是-7(使用DateTime类)。但我需要将其改为“-0700”格式出于实时连接的目的,因此需要所有额外的代码。我希望“-0700”是某种标准格式,这样我就可以以某种方式使用预定义的类。谢谢!我说我真的喜欢PHP是老生常谈吗?
$gmt_offset = $dateTime->format('O');