1. 程式人生 > 實用技巧 >獲取本地時間與指定城市的時差

獲取本地時間與指定城市的時差

$longitude = $info['longitude'];
        $latitude = $info['latitude'];

        //通過Googled api 根據經緯度獲取當地時間與utc國際時間差
        $url = "https://maps.googleapis.com/maps/api/timezone/json?location=$longitude,$latitude&timestamp=1331161200&language=en&key=你的key";

        $curl = curl_init();

        
//設定抓取的url curl_setopt($curl, CURLOPT_URL, $url); //設定標頭檔案的資訊作為資料流輸出 curl_setopt($curl, CURLOPT_HEADER, 1); //設定獲取的資訊以檔案流的形式返回,而不是直接輸出。 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //執行命令 $data = curl_exec($curl); //關閉URL請求 curl_close($curl
); if ($data == '' or $data == null){ return 'err';die(); } $data = substr($data,1,-1); $data = '{'.explode('{',$data)[1]; $data = json_decode($data,true); if (!isset($data['rawOffset'])){ return 'err';die(); }
//指定地點utc時差 $time = $data['rawOffset']; //獲取gmt世界標準時間 $GMT = strtotime(gmdate("Y-m-d H:i:s")); //本地時間 - (國際時間 + 通過api獲取指定地點與國際時間的時區差 【注:結果為指定城市的當前時間】)【注:結果為本地當前時間與指定地點相差時區】(utc即本地時間為更精準的gmt) $gmp = time() - ($GMT + $time);      //時差