计算两个日期和小时之间的时差分钟和天 [英] Calculate the hours minutes and days of difference between two date and hours

查看:60
本文介绍了计算两个日期和小时之间的时差分钟和天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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>';}

这篇关于计算两个日期和小时之间的时差分钟和天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆