PHP - DateTime gmt偏移到浮动格式

时间:2017-05-22 15:57:23

标签: php date datetime

$gmt_offset = get_option("gmt_offset",""); // 3.5 for example
$timezone_string = get_option("timezone_string",""); // America/Caracas for example
if(!empty($timezone_string)){
    $tt = new DateTimeZone($timezone_string);
    $time = new DateTime('now', $tt);
    $gmt_offset = $time->format('O');
 }

    return $gmt_offset;

我试图以浮动格式获取gmt偏移量,其中

3.5小时表示为(浮动)3.5

3小时15分钟(浮动)3.25

问题是DateTime :: format()函数没有这样的格式,我找不到任何相关的问题。

2 个答案:

答案 0 :(得分:0)

这是一种方法:

** EwsRequestHttpHeaders **
<Trace Tag="EwsRequestHttpHeaders" Tid="19" Time="2017-05-22 15:28:20Z">
POST /EWS/Exchange.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
Accept: text/xml
User-Agent: ExchangeServicesClient/15.00.0913.015
Accept-Encoding: gzip,deflate
X-AnchorMailbox: mail@contoso.onmicrosoft.com
X-PreferServerAffinity: True
X-BackEndOverrideCookie:


</Trace>

** EwsRequest **
<Trace Tag="EwsRequest" Tid="19" Time="2017-05-22 15:28:20Z" Version="15.00.0913.015">
  <?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
      <t:RequestServerVersion Version="Exchange2013" />
      <t:ExchangeImpersonation>
        <t:ConnectingSID>
          <t:SmtpAddress>mail@contoso.onmicrosoft.com</t:SmtpAddress>
        </t:ConnectingSID>
      </t:ExchangeImpersonation>
    </soap:Header>
    <soap:Body>
      <m:GetStreamingEvents>
        <m:SubscriptionIds>
          <t:SubscriptionId>JwBk...iMM=</t:SubscriptionId>
        </m:SubscriptionIds>
        <m:ConnectionTimeout>30</m:ConnectionTimeout>
      </m:GetStreamingEvents>
    </soap:Body>
  </soap:Envelope>
</Trace>

** EwsResponseHttpHeaders **
<Trace Tag="EwsResponseHttpHeaders" Tid="19" Time="2017-05-22 15:28:21Z">
HTTP/1.1 200 OK
Transfer-Encoding: chunked
request-id: 25bb05d0-7619-419d-833b-e0b7f3b539c5
X-CalculatedBETarget: DB5PR02MB1191.eurprd02.prod.outlook.com
X-BackEndHttpStatus: 200
X-NoBuffering: 1
X-DiagInfo: DB5PR02MB1191
X-BEServer: DB5PR02MB1191
X-FEServer: HE1P195CA0011
Cache-Control: private
Date: Mon, 22 May 2017 15:28:20 GMT
Set-Cookie: exchangecookie=4a58...bf16; path=/
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET


</Trace>

答案 1 :(得分:0)

$gmt_offset = get_option("gmt_offset",""); // 3.5 for example
$timezone_string = get_option("timezone_string",""); // America/Caracas for example
if(!empty($timezone_string)){
    $tt = new DateTimeZone($timezone_string);
       $time = new DateTime('now',$tt);
      $gmt_offset_h = $time->format('H');
      $gmt_offset_m = (int)($time->format('i')*100/60);
      $gmt_offset = $gmt_offset_h .'.'. $gmt_offset_m;
 }

     return  $gmt_offset ;

希望这会对你有所帮助

编辑:在if条件中添加$ gmt_offset,如果值$ timezone_string为空则返回$ gmt_offset的有效值