这个问题非常奇葩,没遇到过的可能这辈子都不会遇到,遇上的折腾来折腾去也不一定能搞定,索性发个教程出来,希望能帮助到后面遇到这个问题的人。我们通过关键字搜索,大致可以搜索到这几种解决办法:
方法一:在wp-includes\functions.php文件里查找下面代码
function current_time( $type, $gmt = 0 ) {
switch ( $type ) {
case ‘mysql’:
return ( $gmt ) ? gmdate( ‘Y-m-d H:i:s’ ) : gmdate( ‘Y-m-d H:i:s’, ( time() + ( get_option( ‘gmt_offset’ ) * 3600 ) ) );
break;
case ‘timestamp’:
return ( $gmt ) ? time() : time() + ( get_option( ‘gmt_offset’ ) * 3600 );
break;
}
}
function current_time( $type, $gmt = 8 ) {
switch ( $type ) {
case ‘mysql’:
return ( $gmt ) ? gmdate( ‘Y-m-d H:i:s’ ) : gmdate( ‘Y-m-d H:i:s’, ( time() + ( get_option( ‘gmt_offset’ ) * 3600 ) ) );
break;
case ‘timestamp’:
return ( $gmt ) ? time() : time() + ( get_option( ‘gmt_offset’ ) * 3600 );
break;
}
}
方法二:按照以下步骤操作
date_default_timezone_set(PRC);
如果能通过上面的办法能解决,那是最好的,恰好我通过上面的办法依然无法解决。
这个问题很多人发过教程,并且大部分都说明了是因为插件导致的时间错乱,所以如果知道是什么插件导致的,直接禁用插件即可解决,但是由于我这问题莫名其妙就出现,所以献上最终解决办法:
把下面的代码添加到当前主题的function文件中即可:
if(!function_exists('cwp_set_current_timezone')){
function cwp_set_current_timezone($timezone){
if( false===stripos( $timezone,':' ) ){
$timezone_name = $timezone;
}else{
$timezone = strtolower($timezone);
$timezone = str_replace(array('utc','UTC'), '', $timezone) ;
$timezone = preg_replace('/[^0-9]/', '', $timezone) * 36;
$timezone_name = timezone_name_from_abbr(null, $timezone, true);
if( false===$timezone_name ){
$timezone_name = timezone_name_from_abbr(null, -10* 3600, false);
}
}
if(function_exists('date_default_timezone_set')){
date_default_timezone_set($timezone_name);
return $timezone_name;
}
return false;
}
}
if(function_exists('date_default_timezone_set')){
$timezone = '+0:00';
cwp_set_current_timezone( $timezone );
}