calculate difference between dates

6

I am trying to calculate the difference between two dates with the format Y-m-d H:i:s for example 2017-04-10 22:39:09 par that shows me the difference of days, hours and minutes. One of the dates will be the one in the BDD and the other the current one what I have at the moment is this:

//Hago la consulta etc... y esta es la variable donde almacenaré la fecha
$fec_ = transcurridoPublicacion($fila['Prod_Fec']);

The function that calculates it:

function transcurridoPublicacion($fecha){
    $fecha2 = date("Y-m-d H:i:s");
    $fechaF = date_diff($fecha, $fecha2);
    return $fechaF;          
}
    
asked by gmarsi 11.04.2017 в 00:22
source

2 answers

5

you can calculate it in the following way:

function transcurridoPublicacion($fecha){
    $fecha1 = date($fecha);
    $fecha2 = new DateTime('2011-01-03 17:13:00');
    $fechaF = $fecha->diff($fecha2);
// Lo siguiente es por si quieres ponerlo ya en texto la diferencia
//  $elapsed = $interval->format('%y años %m meses %a dias %h horas %i minutos %S segundos');
//  return $elapsed;
    return $fechaF;          
}
    
answered by 11.04.2017 / 00:47
source
3

You can try something like this:

$fechaF = $fecha->diff($fecha2);

Same here I leave the documentation.

And you print it:

print $fechaF->format("%H:%I:%S"); 
    
answered by 11.04.2017 в 00:28