donglu8549 2015-11-05 14:42
浏览 17
已采纳

偏移设置中的时区下拉值

I need a user to be able to select a timezone for, lets say, some "broadcast". I need to save only the timezone_offset value of the selected timezone BUT in seconds. Like:

+03:00 should be save to the database like 10800 , or if -03:00 this should be save like -10800

so + offtests should be saved as just the number in seconds without the plus sign and - offsets should be saved with the minus sign -10800

I found this function:

<?php
/**
 * Timezones list with GMT offset
 *
 * @return array
 * @link http://stackoverflow.com/a/9328760
 */
function tz_list() {
  $zones_array = array();
  $timestamp = time();
  foreach(timezone_identifiers_list() as $key => $zone) {
    date_default_timezone_set($zone);
    $zones_array[$key]['zone'] = $zone;
    $zones_array[$key]['diff_from_GMT'] = 'UTC/GMT ' . date('P', $timestamp);
  }
  return $zones_array;
}
?>

So this:

<div style="margin-top: 20px;">
  <select style="font-family: 'Courier New', Courier, monospace; width: 450px;">
    <option value="0">Please, select timezone</option>
    <?php foreach(tz_list() as $t) { ?>
      <option value="<?php print $t['zone'] ?>">
        <?php print $t['diff_from_GMT'] . ' - ' . $t['zone'] ?>
      </option>
    <?php } ?>
  </select>
</div>

gives me this:

                <option value="Africa/Abidjan">
                UTC/GMT +00:00 - Africa/Abidjan </option>
                    <option value="Africa/Accra">
                UTC/GMT +00:00 - Africa/Accra </option>
                    <option value="Africa/Addis_Ababa">
                UTC/GMT +03:00 - Africa/Addis_Ababa </option>
                    <option value="Africa/Algiers">
                UTC/GMT +01:00 - Africa/Algiers </option>

But I would need the values to be 10800 or -10800 depending on the selected timzone.

My environment is laravel 5.1* so I also have carbon available maybe that could help.

So the essential question is, how to convert timezone offset format "+03:00" to "10800" and "-03:00" to "-10800"

  • 写回答

1条回答 默认 最新

  • dongtao4787 2015-11-05 15:10
    关注

    You can get the time zone's offset by utilizing PHP's native DateTimeZone object. Here's updated tz_list():

    function tz_list() {
      $zones_array = array();
      $timestamp = time();
      $dummy_datetime_object = new DateTime();
      foreach(timezone_identifiers_list() as $key => $zone) {
        date_default_timezone_set($zone);
        $zones_array[$key]['zone'] = $zone;
        $zones_array[$key]['diff_from_GMT'] = 'UTC/GMT ' . date('P', $timestamp);
    
        $tz = new DateTimeZone($zone);
        $zones_array[$key]['offset'] = $tz->getOffset($dummy_datetime_object);
      }
    
      return $zones_array;
    }
    

    Use offset as value for your select's options.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)