php 7.1中遇到非数字值错误

时间:2017-08-27 03:59:59

标签: php

升级到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 );

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

当您执行以下操作时,

PHP 7 stritcly chek数据类型:您可以更改以下函数

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 );