Error in PHP ... Fatal error: Uncaught Error: Call to a Member Function Modify() on Boolean

Asked

Viewed 1,358 times

2

Guys I have a PHP script that adds another 10 days on a date. The script was working wonderfully well, however, a few days ago I started getting the following error:

PHP Fatal error: Uncaught Error: Call to a Member Function Modify() on Boolean ... line 25.

Row 25 referred to in the error is this:

$dt_mod1->modify('+10 days');

Here is the complete section related to this line:

$dt_mod1 = DateTime::createFromFormat('d/m/Y H:i:s', $dt_mod);
$dt_mod1->modify('+10 days');
$dt_mod2 = $dt_mod1->format('d/m/Y');
if ($dt_mod2 == $data_atual ){

    mysqli_query($conexao, "UPDATE `cadastro` SET `situacao` = 'CANCELADA' WHERE `cadastro`.`codigo` = $codigo");
    mysqli_query($conexao, "UPDATE `cadastro` SET `dt_mod` = '$data_atual' WHERE `cadastro`.`codigo` = $codigo");
}

1 answer

6


Basically you passed an invalid value to the function and did not test.

Ideally your code would have something like this:

$dt_mod1 = DateTime::createFromFormat('d/m/Y H:i:s', $dt_mod);
if ( $dt_mod1 ) {
   // faz o que tem que fazer
   $dt_mod1->modify('+10 days');
   $dt_mod2 = $dt_mod1->format('d/m/Y');
   // etc
} else {
   // Trata o erro
}


Understanding the problem

See this detail of the manual, which I marked in bold:

Value Returned
Returns a new instance of Datetime or FALSE in case of failure.

That’s what happens in your case, and FALSE has not the method modify. Precisely for this reason the error mentioned.

If you want details of the error, PHP has a function that you can use inside the else:

if ( $dt_mod1 ) {
   // faz o que tem que fazer
   $dt_mod1->modify('+10 days');
   $dt_mod2 = $dt_mod1->format('d/m/Y');
   // etc
} else {
   echo 'Não foi possível a conversão: ';
   echo htmlentities( date_get_last_errors() );
}


Links to the manual:

https://secure.php.net/manual/en/datetime.getlasterrors.php

https://secure.php.net/manual/en/datetime.createfromformat.php

  • Thanks a lot for your help. I realized that I had made a modification to the main code that recorded the date in the format at’d/m/Y to s H:i:s' and forgot to modify it in the code that was’d/m/Y H:i:s'.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.