Show with php the difference in hours and minutes between two datetime fields of mysql

-1

Good afternoon everyone,

I have a system where I must control the time of entry and exit of vehicles. To do this, I have two DateTime fields stored in Mysql (date1, date2). I need, in PHP, calculate the difference between both fields and show it hours and minutes and seconds, knowing that date1 is the time of entry and date2 the output. Thank you in advance for the help.

    
asked by Filino 15.08.2017 в 00:17
source

2 answers

1

Have you tried the class DateInterval ?

Allows you to compare two dates and an object of type DateInterval returns to you.

Example:

// Traducción del ejemplo del usuario Learner (publicado: Dec 1, 2016)
// Post Original: "calculate the difference between 2 timestamps in php";
// https://stackoverflow.com/questions/40905174/calculate-the-difference-between-2-timestamps-in-php

$fecha1 = new DateTime('2016-11-30 03:55:06');//fecha inicial
$fecha2 = new DateTime('2016-11-30 11:55:06');//fecha de cierre

$intervalo = $fecha1->diff($fecha2);

echo $intervalo->format('%Y años %m meses %d days %H horas %i minutos 
%s segundos');//00 años 0 meses 0 días 08 horas 0 minutos 0 segundos
    
answered by 15.08.2017 / 00:45
source
0

Excellent helped me a lot.

If you are using laravel (my case) to calculate the dates it is practically the same only that it includes DATETIME after your namespace App\Http\Controllers;

use DateTime;

You can now use it on your controller. This is useful when you want to know how long it takes to execute a function if you are doing import process or complex analysis.

$fecha1 = new DateTime();//fecha inicial

/*
*Aquí pones tu código o anidación de funciones.
*/

$fecha2 = new DateTime();//fecha de cierre
$intervalo = $fecha1->diff($fecha2);
echo ' Tiempo de ejecución: '.$intervalo->format('%i minutos %s segundos');

The echo you divert it in the request AJAX as the answer you print it in some modal and ready you see flirtatious.

    
answered by 05.09.2018 в 23:21