Anu
Anu

Reputation: 925

Total hours and difference from two times in PHP

i want one feature,that employee can apply for permission in a month not exceeding 4 hrs in total for a month. So before applying this, im checking database ,and totalling all hours sum of all ( totime- fromtime). and checking whether the hours is more than 4. I tried below code..but exactly i cannot get my need..

$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;         
    }

If the month while applying is july, then im checking and getting all entries in july month for that employee in database,and accumulating it and finally i have to check that.If the hour is morethan 4 hrs, then he cannot apply permission.

Edited: My requirement is, i want the total hours taken per month in Hours,Minute format. Database:

 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

Upvotes: 0

Views: 43

Answers (2)

jeroen
jeroen

Reputation: 91734

There are a few problems here:

  • You are not restricting your query to the correct month and year. You are checking for the month later on, but if it was already assigned in the same month last year (or any of the previous...), this year your employee will get nothing or at least a less than 4. Restricting it in your sql query will solve that and only give you the rows you need (more efficient);
  • strtotime() will give you seconds since 1 January 1970 so if you use these values, you will have it in seconds, not in hours;
  • You have syntax errors, for example the missing ) on the $diff = ... line.

Upvotes: 1

Nitin Pund
Nitin Pund

Reputation: 1092

In your code $amount is in "strtotime" format, I think you need to convert it into hrs..

Upvotes: 0

Related Questions