【问题标题】:Javascript Timezone get Location similar to PHP DateTimeZone::getLocation?Javascript 时区获取类似于 PHP DateTimeZone::getLocation 的位置?
【发布时间】:2015-05-21 09:08:26
【问题描述】:

是否有人知道与 PHP DateTimeZone::getLocation - http://php.net/manual/en/datetimezone.getlocation.php 的功能相同但用于 Javascript(或 NodeJS NPM)的库、函数或 API。

请注意,不要仅仅为了给我指明正确的方向而寻找代码。尝试寻找类似的东西可以找到任何东西。

例如-

我想采用 UTC 偏移量或时区:

utc_offset: -25200,
time_zone: 'Pacific Time (US & Canada)',

然后基于此返回位置/地理位置:

country_code: CZ
latitude: 50.08333
longitude: 14.43333

【问题讨论】:

    标签: javascript node.js geolocation timezone location


    【解决方案1】:

    由于时间紧迫,我决定基于 PHP DateTimeZone::getLocation 函数制作一个小型 API。我可能会在稍后阶段将其设为公共 API。

    这里也是要下载的 gitHub: https://github.com/sputn1k/timezoneGetLocationAPI/

    <?php
    
    //usage: /?timezone=Europe/Prague
    //output(JSON): {"country_code":"CZ","latitude":50.08333,"longitude":14.43333,"comments":""}
    
    if(isset($_GET['timezone'])) {
    
        $timezone = $_GET['timezone'];
    
        try {
    
            $timezone = new DateTimeZone($timezone);
            output(timezone_location_get($timezone));
    
        } catch(Exception $error) {
            output(array('error' => 'bad timezone')); //or add $error for full message 
        }
    }
    
    function output($data){
        echo json_encode($data);
    }
    
    ?>
    

    【讨论】:

      【解决方案2】:

      您可以使用 JodaAPI。 http://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeZone.html 希望对你有帮助^^

      【讨论】:

      • 谢谢@thanhnd25,如果我发现使用这个,我会看看并标记为答案,干杯
      • 刚刚快速浏览了一下,JodaAPI 似乎是“Java”API 而不是“Javascript”?
      • 谢谢我稍后再看,我刚刚基于 PHP DateTimeZone::getLocation 做了一个小的 Javascript API。
      猜你喜欢
      • 2019-12-10
      • 2012-08-27
      • 1970-01-01
      • 2015-12-02
      • 2010-11-29
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      相关资源
      最近更新 更多