2
$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;

I am trying to get gmt offset in float format, where

3.5 hour is represented as (float)3.5

3 hours 15 minutes is (float)3.25

The problem is that DateTime::format() function does not have such format, and I could not find any relevant question.

2
  • Just split the returned value, divide the minutes digits by 60 and then add it to the hours
    – Mark Baker
    May 22, 2017 at 16:03
  • 1
    Looks like I found the way by doing $gmt_offset = $time->format('Z')/3600;
    – Mathew
    May 22, 2017 at 16:04

2 Answers 2

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 ;

hope this will help you

Edit: Added $gmt_offset in if condition to return valid value of $gmt_offset if value $timezone_string is empty

0

Here's one way to do it:

// 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 ) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.