Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
UTC问题,不确定如何解决(PHP/MySQL)_Php_Mysql_Timezone - Fatal编程技术网

UTC问题,不确定如何解决(PHP/MySQL)

UTC问题,不确定如何解决(PHP/MySQL),php,mysql,timezone,Php,Mysql,Timezone,我在我的服务器上的所有日期/时间字段中看到UTC日期和时间,我将其全部返回到我的Web服务器上的时区设置,即UTC时间。。。我的问题是,如何将这些数据转换为本地用户的日期和时间?所有时间都存储在MySQL服务器上的UTC中 我有城市、地区/州和国家的用户。最糟糕的情况是,我想在PHP默认时区中显示日期和时间 如何做到这一点?在MySQL中,当您从数据库中选择数据时,可以使用该函数将所有日期转换为当前时区。另一个stackoverflow线程讨论您可以使用此函数,并将用户的位置(澳大利亚华盛顿州珀

我在我的服务器上的所有日期/时间字段中看到UTC日期和时间,我将其全部返回到我的Web服务器上的时区设置,即UTC时间。。。我的问题是,如何将这些数据转换为本地用户的日期和时间?所有时间都存储在MySQL服务器上的UTC中

我有城市、地区/州和国家的用户。最糟糕的情况是,我想在PHP默认时区中显示日期和时间


如何做到这一点?

在MySQL中,当您从数据库中选择数据时,可以使用该函数将所有日期转换为当前时区。另一个stackoverflow线程讨论

您可以使用此函数,并将用户的位置(澳大利亚华盛顿州珀斯)作为地址传递给它

您可以让它返回服务器时区和默认php时区之间的偏移量,而不是在失败时返回0

您可能希望将其存储在会话变量中,并且仅在变量为空时调用该函数,这将提高页面呈现的速度

/**
 * 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;
}
代码改编自一个LGPL php类