2017-08-27 27 views
1

非數值出現的錯誤,我有這個錯誤後升級到PHP 7.1在PHP 7.1

PHP Warning: A non-numeric value encountered in public_html/wp-includes/formatting.php on line 3221 

這裏該文件的代碼。

function human_time_diff($from, $to = '') { 
if (empty($to)) { 
    $to = time(); 
} 
//line 3221 
$diff = (int) abs($to - $from); 
+0

是從約會?在這種情況下,from是一個字符串,你可以減去字符串或字符串和整數 – Andreas

回答

1

PHP 7,當你做這樣的操作stritcly赤數據類型:您可以更改功能如下

function human_time_diff($from, $to = '') { 
if (empty($to) || ! is_numeric($to)) { 
    $to = time(); 
} 
//check for from may be its valid date format but not time stamp 
if(! is_numeric($from)) { 
    if(strtotime($from)){ 
     $from = strtotime($from); 
    } 
    else{ 
     return 'Error: In valid from date.'; 
    } 
} 
//line 3221 
$diff = (int) abs($to - $from); 
2

只要做一點點更多的檢查,看看是否變量數值:

function human_time_diff($from, $to = '') 
{ 
    if(! is_numeric($to) OR empty($to)) { 
     $to = time(); 
    } 

    if(! is_numeric($from)) { 
     return 'Error: From must be numeric.'; 
    } 

    $diff = (int) abs($to - $from); 

    return $diff; 
}