2009-06-18 36 views
0

我看到我的服務器上的所有日期/時間字段中的UTC日期和時間,我將它們全部返回到我的web服務器上的時區設置UTC時間...我的問題是如何讓這些成爲本地用戶的日期和時間?所有的時間都在MySQL服務器上以UTC存儲。UTC問題,不知道如何解決它們(PHP/MySQL)

我有用戶城市,地區(省/州)和國家。最壞情況我想在PHP默認時區顯示日期和時間。

我該怎麼做?

回答

1

您可以使用此函數,並將其傳遞給用戶的位置「澳大利亞西部珀斯」作爲地址。

而不是在失敗時返回0,您可以讓它返回服務器時區和默認php時區之間的偏移量。

您可能希望將其存儲在會話變量中,並且只在變量爲空時才調用函數,這可以提高頁面渲染的速度。

/** 
* This function finds the geocode of any given place using the geocoding service of yahoo 
* @example getGeoCode("White House, Washington"); 
* @example getGeoCode("Machu Pichu"); 
* @example getGeoCode("Dhaka"); 
* @example getGeoCode("Hollywood"); 
* 
* @param string address - something you could type into Yahoo Maps and get a valid result 
* @return int timeZoneOffset - the number of seconds difference between the user's timezone and your server's timezone, 0 if it fails. 
*/ 
function getTimeZoneOffset($address) { 
    $_url = 'http://api.local.yahoo.com/MapsService/V1/geocode'; 
    $_url .= sprintf('?appid=%s&location=%s',"phpclasses",rawurlencode($address)); 
    $_result = false; 
    if($_result = file_get_contents($_url)) { 
     preg_match('!<Latitude>(.*)</Latitude><Longitude>(.*)</Longitude>!U', $_result, $_match); 
     $lng = $_match[2]; 
     $lat = $_match[1]; 

     $url = "http://ws.geonames.org/timezone?lat={$lat}&lng={$lng}"; 
     $timedata = file_get_contents($url); 
     $sxml = simplexml_load_string($timedata); 
     $timeZoneOffset = strtotime($sxml->timezone->time) - time(); 
     return $timeZoneOffset; 
    } 
    else 
    return 0; 
} 

代碼改編自Time Engine,一個LGPL php類。

相關問題