1

I would like to calculate working hours between two dates. I am using working_hours_diff function which is answered here; https://stackoverflow.com/a/8927347/4671728

Actually, it is calculating normally but when I input two same date but different hours, it returns wrong values. For example; 29-12-2015 13:17:43 and 29-12-2015 11:17:39 - It returns 11 hours but it should be returned 2 hours. However when I input different dates from eachother, it calculates correctly.

You can kindly find the codes below;

function work_hours_diff($date1,$date2) {
if ($date1>$date2) { $tmp=$date1; $date1=$date2; $date2=$tmp; unset($tmp); $sign=-1; } else $sign = 1;
if ($date1==$date2) return 0;

$days = 0;
$working_days = array(1,2,3,4,5); // Monday-->Friday
$working_hours = array(8, 17); // from 8:30(am) to 17:30
$current_date = $date1;
$beg_h = floor($working_hours[0]); $beg_m = ($working_hours[0]*60)%60;
$end_h = floor($working_hours[1]); $end_m = ($working_hours[1]*60)%60;

// setup the very next first working timestamp

if (!in_array(date('w',$current_date) , $working_days)) {
    // the current day is not a working day

    // the current timestamp is set at the begining of the working day
    $current_date = mktime( $beg_h, $beg_m, 0, date('n',$current_date), date('j',$current_date), date('Y',$current_date) );
    // search for the next working day
    while ( !in_array(date('w',$current_date) , $working_days) ) {
        $current_date += 24*3600; // next day
    }
} else {
    // check if the current timestamp is inside working hours

    $date0 = mktime( $beg_h, $beg_m, 0, date('n',$current_date), date('j',$current_date), date('Y',$current_date) );
    // it's before working hours, let's update it
    if ($current_date<$date0) $current_date = $date0;

    $date3 = mktime( $end_h, $end_m, 59, date('n',$current_date), date('j',$current_date), date('Y',$current_date) );
    if ($date3<$current_date) {
        // outch ! it's after working hours, let's find the next working day
        $current_date += 24*3600; // the day after
        // and set timestamp as the begining of the working day
        $current_date = mktime( $beg_h, $beg_m, 0, date('n',$current_date), date('j',$current_date), date('Y',$current_date) );
        while ( !in_array(date('w',$current_date) , $working_days) ) {
            $current_date += 24*3600; // next day
        }
    }
}

// so, $current_date is now the first working timestamp available...

// calculate the number of seconds from current timestamp to the end of the working day
$date0 = mktime( $end_h, $end_m, 59, date('n',$current_date), date('j',$current_date), date('Y',$current_date) );
$seconds = $date0-$current_date+1;

//printf("<br>From %s To %s : %d hours<br>",date('d/m/y H:i',$date1),date('d/m/y H:i',$date0),$seconds/3600);

// calculate the number of days from the current day to the end day

$date3 = mktime( $beg_h, $beg_m, 0, date('n',$date2), date('j',$date2), date('Y',$date2) );
while ( $current_date < $date3 ) {
    $current_date += 24*3600; // next day
    if (in_array(date('w',$current_date) , $working_days) ) $days++; // it's a working day
}
if ($days>0) $days--; //because we've allready count the first day (in $seconds)

//printf("<br>From %s To %s : %d working days<br>",date('d/m/y H:i',$date1),date('d/m/y H:i',$date3),$days);

// check if end's timestamp is inside working hours
$date0 = mktime( $beg_h, 0, 0, date('n',$date2), date('j',$date2), date('Y',$date2) );
if ($date2<$date0) {
    // it's before, so nothing more !
} else {
    // is it after ?
    $date3 = mktime( $end_h, $end_m, 59, date('n',$date2), date('j',$date2), date('Y',$date2) );
    if ($date2>$date3) $date2=$date3;
    // calculate the number of seconds from current timestamp to the final timestamp
    $tmp = $date2-$date0+1;
    $seconds += $tmp;
    //printf("<br>From %s To %s : %d hours<br>",date('d/m/y H:i',$date2),date('d/m/y H:i',$date3),$tmp/3600);
}

// calculate the working days in seconds

$seconds += 3600*($working_hours[1]-$working_hours[0])*$days;

//printf("<br>From %s To %s : %d hours<br>",date('d/m/y   H:i',$date1),date('d/m/y H:i',$date2),$seconds/3600);

//return $sign * $seconds/3600; // to get hours
return round($seconds/3600);
}

date_default_timezone_set("Europe/Istanbul");
$dt2 = strtotime("29-12-2015 11:17:39");
$dt1 = strtotime("29-12-2015 13:17:43");
echo work_hours_diff($dt1 , $dt2 );
Community
  • 1
  • 1

3 Answers3

1

Replace this line:

if ($days>0) $days--; //because we've allready count the first day (in $seconds)

with:

$days--; // because we've already counted the first day (in $seconds)

That if was bad, as it removed the distinction between $days values of 0 and 1 (before the if), which just cannot be right.

trincot
  • 317,000
  • 35
  • 244
  • 286
0

Try this..

date_default_timezone_set("Europe/Istanbul");
$dt2 = strtotime("29-12-2015 11:17:39");
$dt1 = strtotime("29-12-2015 13:17:43");
echo $diff_hours = ($dt1 - $dt2)/3600;

Output

2.0011111111111 hours
Amit Rajput
  • 2,061
  • 1
  • 9
  • 27
  • Thanks for your answer, but I have to calculate without weekend and just include working hours between dates. Your codes are just calculating all hours between dates. work_hours_diff function is working for me but when I enter same date with different hours, it does not calculate correctly. However if I enter different dates it calculates correctly. I just want to fix this problem. –  Jan 05 '16 at 08:54
0

Replace

if ($days>0) $days--; //because we've allready count the first day (in $seconds)

For this:

if (in_array(date('w',$date2) , $working_days)) {
    $days--; //because we've allready count the first day (in $seconds)
}
Ramon Lucas
  • 167
  • 1
  • 4