2 votes

Fatal error: Call to a member function diff() on string in

I want to see the difference in days between the current date and the input data, but I get this error.

Fatal error: Call to a member function diff() on string in

This is the code:

<input type="date" class="datapicker" required="" name="VEHICLE_DATE_EXPIRATION_SURELY" value="<?php $fechabd1= $fila["VEHICLE_DATE_EXPIRATION_SURELY"];

if($fechabd1==""){

    }
else{
        echo $fechabd1->format('Y-m-d');
    }
 ?>" min="<?php date_default_timezone_set('America/Bogota'); $fechaactual1 = date("Y-m-d");ECHO $fechaactual1; ?>">
 <?php 
    $fechahoy=date("Y/m/d");
    $interval = $fechahoy->diff($fechabd1);
    echo $interval->format('%R%a días');
  ?>

3voto

Marcos Points 15902

We have that the method date

Returns a formatted date string. If a non-numeric value is used for timestamp is returned FALSE and a level error is issued E_WARNING .

It is precisely for this reason that when making:

$fechahoy=date("Y/m/d");
$interval = $fechahoy->diff($fechabd1);

You get the error.

Solution:

You should use DateTime to create the variable $fechahoy and be able to use diff .

Example:

$fechahoy = new DateTime();
$interval = $fechahoy->diff($fechabd1);
echo $interval->format('%R%a días');

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X