Get timezone offset for a given location

前端 未结 3 2114
南旧
南旧 2021-02-09 08:13

Is it possible in PHP to get the timezone offset for a given location? E.g. when given the location \"Sydney/Australia\" to get the timezone offset as \"+1100\". Bonus would be

3条回答
  •  Happy的楠姐
    2021-02-09 08:49

    Not sure why you need "+1100" (rather than a decimal representation) but you can use this:

    $dt = new DateTime(null, new DateTimeZone('Australia/Sydney'));
    $offset = $dt->getOffset()/60/60; // 11
    
    $hours = intval($offset);
    $minutes = str_pad((string)($offset - $hours) * 60, 2, '0', STR_PAD_RIGHT);
    echo $hours.$minutes; // 1100
    

    Replace null with '2010-10-01' and you'll get 1000

提交回复
热议问题