Mostafa
Mostafa

Reputation: 197

A non-numeric value encountered error in php 7.1

i have this error after upgrade to php 7.1

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

here the code on that file.

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

Upvotes: 1

Views: 1368

Answers (2)

B. Desai
B. Desai

Reputation: 16436

PHP 7 stritcly chek datatype when you do operations like this: You can change function as below

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

Upvotes: 1

Brian Gottier
Brian Gottier

Reputation: 4582

Just do a little more checking to see if the variables are numeric:

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

Upvotes: 2

Related Questions