[英]Get time difference only from hours and minutes
I have two times one is $InTime
: 12:55:10
and the other is $OutTime
: 12:56:10
我有两次一个是
$InTime
: 12:55:10
,另一个是$OutTime
: 12:56:10
So I think calculating the time difference should be: 因此,我认为计算时差应为:
$TotalTimeInOffice = $OutTime - $InTime
I want in hours so I think this should be the result 00:01
, but I am getting wrong result. 我想要几个小时,所以我认为应该是
00:01
的结果,但是我得到的结果是错误的。 With this calculation I am get 0
in the result, but why? 通过这种计算,我得到的结果为
0
,但是为什么呢?
Here are some examples from database: 以下是数据库中的一些示例:
InTime is: 12:55:10
OutTime is: 12:56:09
Result Is: 0
InTime is: 12:24:45
OutTime is: 00:00:00
Result Is: -12
InTime is: 10:05:48
OutTime is: 10:06:11
Result Is: 0
and the PHP Code would be. 和PHP代码。
$timestart = $time_sheets->in_time;
$timestop = $time_sheets->out_time;
$time_diff = $timestop - $timestart ;
echo "InTime is: $timestart <br />";
echo "OutTime is: $timestop <br />";
print_r('Result Is: '.$time_diff."<br />");
Your code fails when midnight is involved eg 12:24:45 - 00:00:00
returns -12:24:45
. 当涉及午夜时,您的代码将失败,例如
-12:24:45
12:24:45 - 00:00:00
: 12:24:45 - 00:00:00
: 12:24:45 - 00:00:00
返回-12:24:45
。 You need to correct the times if they are located on either side of midnight: 如果它们位于午夜的两边,则需要更正时间:
function getDuration($timestart, $timestop) {
$time1 = new DateTime("today " . $timestart);
$time2 = new DateTime("today " . $timestop);
if ($time2 < $time1) {
$time2 = new DateTime("tomorrow " . $timestop);
}
$time_diff = $time1->diff($time2);
echo " InTime is: $timestart\n";
echo "OutTime is: $timestop\n";
echo " Result is: " . $time_diff->format("%r%H:%I:%S") . "\n\n";
}
getDuration("12:55:10", "12:56:09");
getDuration("12:24:45", "00:00:00");
getDuration("10:05:48", "10:06:11");
getDuration("11:00:00", "01:00:00");
Output: 输出:
InTime is: 12:55:10
OutTime is: 12:56:09
Result is: 00:00:59
InTime is: 12:24:45
OutTime is: 00:00:00
Result is: 11:35:15
InTime is: 10:05:48
OutTime is: 10:06:11
Result is: 00:00:23
InTime is: 11:00:00
OutTime is: 01:00:00
Result is: 14:00:00
I guess you should convert it to time() using strtotime(); 我猜您应该使用strtotime()将其转换为time(); then use $timeOutTime - $timeInTime;
然后使用$ timeOutTime-$ timeInTime;
$timestart = $time_sheets->in_time;
$timestop = $time_sheets->out_time;
$timestart = strtotime($timestart);
$timestop = strtotime($timestop);
$time_diff = $timestop - $timestart ;
echo data("h:i:s",$time_diff);
this should work. 这应该工作。
// Change It
$time1 = new DateTime($timestart);
$time2 = new DateTime($timestop);
// $time_diff = $timestop - $timestart ;
$time_diff = $time1->diff($time2);
echo "InTime is: $timestart <br />";
echo "OutTime is: $timestop <br />";
print_r('Result Is: ' . $time_diff->format('%H:%I:%s') . "<br />");
// End of Change
Problem Solved with DateTime function, but if there is any other good/better way then please post that answer too.. 使用DateTime函数解决了问题,但是如果还有其他更好/更好的方法,请也发布该答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.