我更喜欢在Unix Epoch中记录所有时间,就像它一样简单
hoursWorked = ((stopTime - startTime)/60)/60
Run Code Online (Sandbox Code Playgroud)
这是一个完整的解决方案,它在PHP中,但应该很容易构建到任何语言:
<?php
$startTime = "12/31/2008 22:02"; //No AM/PM we'll use 24hour system
$endTime = "01/01/2009 06:27"; //Again no AM/PM and we spaned the midnight time gap.
/*
Use this to test for a normal shift not ocurring durring midnight.
$startTime = "01/01/2008 06:02"; //No AM/PM we'll use 24hour system
$endTime = "01/01/2008 14:27"; //Again no AM/PM and we spaned the midnight time gap.
*/
$startTime = split(' ', $startTime);
$endTime = split(' ', $endTime);
$startTime[1] = split(':', $startTime[1]);
$endTime[1] = split(':', $endTime[1]);
/*
$startTime[0] contains the date
$startTime[1][0] contains the hours
$startTime[1][1] contains the minutes
same is true for endTime
*/
if($startTime[0] != $endTime[0])
{
if(date2epoch($endTime[0]) > date2epoch($startTime[0]))
{
$minutesWorked1 = (59 - $startTime[1][1]); //Calculate how many minutes have occured from begining of shift to midnight
$minutesWorked2 = $endTime[1][1]; //Number of minute from midnight to end of shift
$hoursWorked1 = (23 - $startTime[1][0]);//Number of hours from start of shift to midnight
$hoursWorked2 = $endTime[1][0];//Number of minutes from midnight to end of shift
echo 'Before midnight you worked ' . $hoursWorked1 . ':' . $minutesWorked1 . "\nAfter midnight you worked " . $hoursWorked2 . ':' . $minutesWorked2 . '.';
}
else
{
//SOMETHING MAJOR BAD HAS HAPPENED WHILE LOGGING THE CLOCKINS AND CLOCKOUTS
}
}
else
{
echo 'Today you worked ' . ($endTime[1][0] - $startTime[1][0]) . ':' . ($endTime[1][1] - $startTime[1][1]);
}
function date2epoch($date, $format='m/d/Y')
{
$date = split('/', $date);
return mktime('0', '0', '0', $date[0], $date[1], $date[2]);
}
?>
Run Code Online (Sandbox Code Playgroud)
在系统中,这显然是一个微不足道的问题,其中起始和结束时间是包含日期和时间的数据结构.但是有很多系统没有这样做.计时系统常常有一条包含日期,开始时间和结束时间的记录.在这种情况下,问题不那么重要.
我想,有两个问题你要问:
第一个问题很容易回答:如果结束时间在开始时间之前,则时间跨度已经过了午夜.
如果是这种情况,那么您有两个要计算的金额.日期发生的时间跨度是从开始时间到午夜之间的时间.下一个日期发生的时间跨度是午夜和结束时间之间的时间(即结束时间).
| 归档时间: |
|
| 查看次数: |
4091 次 |
| 最近记录: |