2009-06-18 97 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类。

相关问题