计算两个日期和小时之间的时差分钟和天

计算两个日期和小时之间的时差分钟和天

问题描述:

我有一个PHP和MySQL代码,应该计算两个日期和小时之间的小时数分钟和天数.它运作良好,仅比正常情况增加了20小时20分钟.然后,我删除了DATE部分,并手动设置了日期和时间,就可以了.

I have a PHP and MySQL code that should calculate the hours minutes and days of difference between two date and hours. It works well, just adding 20 hours and 20 minutes more than normal. And I remove the DATE part and put the date and time manually, it works fine.

我不知道会发生什么.

    $fecha = $row["fecha"];
        $data= $row["hora"];
$start = strtotime("$fecha $hora"); 


$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate =  date("Y-m-d H:i:s", strtotime($currentDate .$currentTime));

$end = strtotime("$currentDate"); 



$totaltime = ($end - $start)  ; 

$hours = intval($totaltime / 3600);   
$seconds_remain = ($totaltime - ($hours * 3600)); 

$minutes = intval($seconds_remain / 60);   
$seconds = ($seconds_remain - ($minutes * 60));
$statusfichaje= $row["status"];
if ($statusfichaje == Start){echo '<td>Trabajando'.$hours.':'.$minutes.':'.$seconds.' </td>';}else{echo '<td>'. $row["status"] .'</td>';}

编辑

开始2019-12-29 21:27:50.结束于2019-12-31 0:51:50 = 47:51:16

Edit

start 2019-12-29 21:27:50 . end 2019-12-31 0:51:50 = 47:51:16

如您所见,它的计算结果很差.

As you can see it calculates badly.

像这样的简单示例即可完成工作:

A simple example like this would do the job :

$mydatetime = new DateTime();
$datefromdb = new DateTime('2018-03-05 10:10:00');
$interval = $mydatetime->diff($datefromdb);
$date_count = $interval->format('%y years %m months %a days %h hours %i minutes %s seconds');
echo $date_count;

这是您应该使用的代码

$fecha = $row["fecha"];
$data= $row["hora"];
$start = strtotime("$fecha $data"); 


$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate =  date("Y-m-d H:i:s", strtotime($currentDate .$currentTime));

$end = strtotime("$currentDate"); 



$totaltime = ($end - $start)  ; 

$hours = intval($totaltime / 3600);   
$seconds_remain = ($totaltime - ($hours * 3600)); 

$minutes = intval($seconds_remain / 60);   
$seconds = ($seconds_remain - ($minutes * 60));
$statusfichaje= $row["status"];
if ($statusfichaje == $start){echo '<td>Trabajando'.$hours.':'.$minutes.':'.$seconds.' </td>';}else{echo '<td>'. $row["status"] .'</td>';}