PHP中的两小时总时数和差异

时间:2015-07-25 09:18:48

标签: php

我想要一个功能,该员工可以在一个月内申请一个月不超过4小时的许可。因此,在应用此之前,我正在检查数据库,并总计所有小时数的总和(从时间到时间)。并检查小时是否超过4.我尝试下面的代码..但我完全不能满足我的需要..

$total = $this->getTotalHours($emp->id,$req->perdate);          
if($total > 4)
        {
            error_log("You cannot apply more than 4 hours");            
        }


private function getTotalHours($empid,$PerDate){
        $amount = 0;
        $newmonth = date('F', strtotime($PerDate));
        $empPer = new EmployeePermission();
            $leaveDays = $empPer->find("employee = ?",array($empid));
            foreach($leaveDays as $leaveDay){
                $month = date('F', strtotime($leaveDay->applydate));
                if($newmonth == $month){                    
                      $diff = (strtotime($leave->totime) - strtotime($leave->fromtime);
                }
                $amount += $diff;                   
            }
        return $amount;         
    }

如果申请的月份是7月份,那么我在7月份检查并获取该员工在数据库中的所有条目,并累积它,最后我必须检查。如果小时超过4小时,那么他就不能申请权限。

编辑: 我的要求是,我想要以小时,分钟格式每月采取的总小时数。 数据库:

 employee   applydate   fromtime    totime         status
    169     2015-07-24  14:08:37    13:08:37        Approved
    111     2015-07-25  11:12:26    11:30:26        Pending
    111     2015-07-25  12:14:13    12:36:13        Pending
    169     2015-07-27  12:00:00    14:30:00        Pending
    169     2015-07-29  14:00:00    15:00:00        Pending
    111     2015-07-27  16:11:26    17:11:26        Pending

2 个答案:

答案 0 :(得分:1)

这里有一些问题:

  • 您不会将查询限制为正确的月份和年份。你正在检查这个月,但如果它已经在去年同一个月(或之前的任何一个......)中分配,今年你的员工将得不到任何东西或至少少于4个。限制它你的SQL查询将解决这个问题,只给你你需要的行(效率更高);
  • strtotime()将从1970年1月1日起给你几秒钟,所以如果你使用这些值,你将在几秒钟内得到它,而不是几个小时;
  • 您有语法错误,例如)行上缺少$diff = ...

答案 1 :(得分:0)

在您的代码中,$ amount是“strtotime”格式,我认为您需要将其转换为小时..