我在服务器上的所有日期/时间字段中看到了UTC日期和时间,我将其全部回到我的网络服务器上的时区设置,处于UTC时间...我的问题是如何将这些设置为本地用户的日期和时间?所有时间都以UTC格式存储在MySQL服务器上。
我有用户城市,地区(Prov / State)和国家/地区。最糟糕的情况我想在PHP默认时区显示日期和时间。
我该怎么做?
答案 0 :(得分: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类。
答案 1 :(得分:0)
在MySQL中,当您从数据库中选择数据时,您可以使用函数CONVERT_TZ将所有日期转换为当前时区。另一个stackoverflow线程讨论how to convert time zones using PHP 5's DateTime class.