0

I would like to calculate some times. I need to know how many worktime hours are between a call entry and now regarding on the worktimes.

for example: call stored yesterday 15:00 worktime ends: 18:00 worktime begins: 08:00 now: 10:00

So i need to know the age my call has during the working hours:

  • call stored >> worktime end: 3h
  • worktimebegin >> now: 2h
  • age: 5h

I would like to use php DateTime.

How would you proceed?

metaxos
  • 151
  • 1
  • 16

1 Answers1

0

I think this can help you out:

//workdays till 18:00 and start 08:00
function calculateWorkHours(DateTime $Start, DateTime $End) {
    //validate given parameters
    if ($Start > $End) throw new Exception('$Start date cannot be later than $End date');

    $hours = 0;

    do {
        //get the current hour
        $currentHour = $Start->format('H');

        //while the $currenthour is lower then 18 and higher than 7
        if($currentHour < 18 && $currentHour >= 8) {
            $hours++;
        }

        $Start->modify('+1 hour');

    } while($End > $Start);

    return $hours;
}

$Start = new DateTime('yesterday 150000');
$End = new DateTime('100000');
echo calculateWorkHours($Start, $End); //returns 5

$Start = new DateTime('yesterday 060000');
$End = new DateTime('120000');
echo calculateWorkHours($Start, $End); //returns 14

$Start = new DateTime('-2 days 150000');
$End = new DateTime('100000');
echo calculateWorkHours($Start, $End); //return 15
Benz
  • 2,317
  • 12
  • 19