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

1

I want to see the difference of 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');
  ?>
    
asked by Sergio 15.01.2018 в 15:33
source

1 answer

3

We have the method date

  

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

It is precisely for this reason that when doing:

$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');
    
answered by 15.01.2018 / 15:47
source