Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/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-日期时间gmt偏移量到浮点格式_Php_Date_Datetime - Fatal编程技术网

PHP-日期时间gmt偏移量到浮点格式

PHP-日期时间gmt偏移量到浮点格式,php,date,datetime,Php,Date,Datetime,我正在尝试以浮点格式获取gmt偏移量,其中 3.5小时表示为(浮动)3.5 3小时15分钟是(浮动)3.25 问题是DateTime::format()函数没有这种格式,我找不到任何相关的问题。这里有一种方法: $gmt_offset = get_option("gmt_offset",""); // 3.5 for example $timezone_string = get_option("timezone_string",""); // America/Caracas for exampl

我正在尝试以浮点格式获取gmt偏移量,其中

3.5小时表示为(浮动)3.5

3小时15分钟是(浮动)3.25


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

这里有一种方法:

$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;
希望这对你有帮助


编辑:在if条件中添加$gmt\u offset,如果值$timezone\u string为空,则返回$gmt\u offset的有效值

只需将返回的值拆分,将分钟数除以60,然后将其添加到小时看起来我找到了方法,方法是执行$gmt\u offset=$time->format('Z')/3600;
// use P (+02:00), in stead of O (+0200), for easier splitting.
$offset_parts = explode( ':', $time->format( 'P' ) );
$gmt_offset = floatval( $offset_parts[ 0 ] . '.' . ( ( $offset_parts[ 1 ] / 60 ) * 100 ) );
$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 ;