简体   繁体   English

正确减去PHP中的时间

[英]Subtracting time in PHP correctly

I have used a few forms to establish a duration of time that a user has worked as well as get the amount of overtime they have worked. 我使用了一些表格来确定用户工作的持续时间以及获得他们工作的加班时间。 Problem is that I want to know how many hours can be billed in overtime and how many is normal time. 问题是我想知道加班可以计算多少小时,以及正常时间多少。 Now its rather obvious, subtract overtime from total time,however I used the following script to obtain the time amounts: 现在它显而易见,从总时间中减去加班,但是我使用以下脚本来获取时间量:

$logtime = new CGenRs("SELECT time_id,
  profile_id ,
  user_number ,
  start_time ,
  end_time,
  description,
  exported,ovt_start_time,ovt_end_time, EXTRACT(hour FROM(end_time - start_time)) as diff,EXTRACT(minute FROM(end_time - start_time))as difference,EXTRACT(hour FROM(ovt_end_time-ovt_start_time)) as ovt_diff,EXTRACT(minute FROM(ovt_end_time-ovt_start_time)) as ovt_difference from adapt_profile_time where exported = 'f' order by user_number", $cao);
$logtime->first();

The Extract feature works well to show the times but when I start to subtract its where it gets a bit messy. 提取功能可以很好地显示时间,但是当我开始减去它有点凌乱的地方时。

$tot_mins = 0;
            $ovt_mins = 0;
        }
        $tot_mins = ($tot_mins + $logtime->valueof('diff') * 60) + $logtime->valueof('difference');
        $ovt_mins = ($ovt_mins + $logtime->valueof('ovt_diff') * 60) + $logtime->valueof('ovt_difference');
    $total_test= floor(($tot_mins / 60)-($ovt_mins / 60))." hours ".(($tot_mins % 60)-($ovt_mins % 60))." minutes ";

When using the echo $total_test it does the calculation but if the user has worked 7 hours 0 minutes which consists out of 3 hours 40 minutes overtime the result of the above calculation returns 3 hours -40 minutes. 当使用echo $total_test进行计算时,如果用户已经工作了7小时0分钟,其中包括超时3小时40分钟,则上述计算的结果返回3小时-40分钟。 Which is wrong. 哪个错了。 So where am I going wrong here? 那我在哪里错了?

I believe the problem lies with the EXTRACT hours and minutes not working well with the "-" "+" operators. 我认为问题在于提取时间和分钟与“ - ”“+”运算符不兼容。 I added a total colum in my table that adds the total time with the overtime (It should be subtracted but I did it to test) I used the following code: 我在我的表中添加了一个总colum,它增加了加班的总时间(应该减去但是我做了测试)我使用了以下代码:

<td><?php echo ($logtime->valueof('diff')   +  $logtime->valueof('ovt_diff')) . " " . "hours" . " ".(($logtime->valueof('difference') + $logtime->valueof('ovt_difference')))." "."minutes"  ?></td>

The result was interesting. 结果很有趣。 If user worked 3 hours 50 minutes of which it all was overtime, the result returned was 6 hours 100 minutes. 如果用户工作3小时50分钟,其中全部是加班,则返回的结果是6小时100分钟。 So the addition is working, its just the values aren't recognized in a time format 因此添加工作正常,其值只是时间格式无法识别

As I stated in the comments, it is wise to use the DateTime classes for any date/time actions you want to do. 正如我在评论中所说,将DateTime类用于您想要执行的任何日期/时间操作是明智的。 Including getting the difference between times. 包括获得时间差异。

Check out this example: 看看这个例子:

// Create two new DateTime-objects...
$Date1 = new DateTime('2015-10-21T8:00:00');
$Date2 = new DateTime('2015-10-21T18:30:00');

// The diff-methods returns a new DateInterval-object...
$Diff = $Date2->diff($Date1);

// Call the format method on the DateInterval-object
echo $Diff->format('%h:%i');

The output from the code above should be "10:30" 上面代码的输出应为“10:30”

From here on you can simply get the difference between the times and check if it is more then 8 hours. 从这里开始,您可以简单地获取时间之间的差异,并检查它是否超过8小时。 If it is, you can get your amount of overtime. 如果是,您可以获得加班费。

I eventually got it working. 我最终得到了它的工作。 I changed my query to the following: 我将查询更改为以下内容:

$logtime = new CGenRs("SELECT time_id,
  profile_id ,
  user_number ,
  start_time ,
  end_time,
  description,
  exported,ovt_start_time,ovt_end_time, EXTRACT(hour FROM(end_time - start_time)) as diff,EXTRACT(minute FROM(end_time - start_time))as difference ,EXTRACT(hour FROM(ovt_end_time-ovt_start_time)) as ovt_diff,EXTRACT(minute FROM(ovt_end_time-ovt_start_time)) as ovt_difference,EXTRACT(EPOCH FROM (end_time - start_time)) as super,EXTRACT(EPOCH FROM (ovt_end_time - ovt_start_time)) as spar FROM adapt_profile_time where exported = 'f' order by user_number", $cao);
$logtime->first();

As you can see I added the EXTRACT Epoch to the query for both the over time and the total work time. 正如您所看到的,我在查询中添加了EXTRACT Epoch ,用于随时间和总工作时间。

I then added the next piece of code: 然后我添加了下一段代码:

<td><?php $epoch_1 = $logtime->valueof('super');
                $epoch_2 = $logtime->valueof('spar');
                $diff_seconds = $epoch_1 - $epoch_2;
                $diff_weeks = floor($diff_seconds / 604800);
                $diff_seconds -= $diff_weeks * 604800;
                $diff_days = floor($diff_seconds / 86400);
                $diff_seconds -= $diff_days * 86400;
                $diff_hours = floor($diff_seconds / 3600);
                $diff_seconds -= $diff_hours * 3600;
                $diff_minutes = floor($diff_seconds / 60);
                $diff_seconds -= $diff_minutes * 60;
                echo $diff_hours." "."hours ".$diff_minutes." minutes" ?></td>
                <td>

And so I got the correct values to display in the table. 所以我得到了正确的值显示在表格中。 That being done I changed the code in my question to the following: 这样做我将问题中的代码更改为以下内容:

$tot_mins = 0;
    $ovt_mins = 0;
    $total_mens = ($logtime->valueof('super') /60 ) + ($logtime->valueof('spar')/60);


    while (!$logtime->eof()) {
        while (!$logtime->eof()) {


            if ($curr_userno != $logtime->valueof('user_number')) {
                $total_time = floor($tot_mins / 60) . " hours " . ($tot_mins % 60) . " minutes";
                $total_ovt = floor($ovt_mins / 60) . " hours " . ($ovt_mins % 60) . " minutes";
                $total_test = floor($total_mens/60)." hours ".($total_mens%60). " minutes";

And now its working.The values are adding up perfectly and carrying over when necessary. 现在它的工作。价值观完美地结合在一起并在必要时继续运作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM