skip to Main Content

I have made a code to calculate the difference in hours between two dates, but I have a problem, holidays are still being calculated.

i want to calculate the difference without following the holidays

here is my code

date_default_timezone_set('Asia/Jakarta');
$start_date  = date_create("2023-08-18 00:00:00");
$end_date = date_create("2023-08-21 00:00:00");
$different  = date_diff( $start_date, $end_date );
$day = $different->d;
$hour = $different->h;
$minute = $different->i;
        
if($hour < 10){
    $this_hour = "0".$hour;
} else {
    $this_hour = $hour;
}
        
if($minute < 10){
    $this_minute = "0".$minute;
} else {
    $this_minute = $minute;
}
        
if($day < 1){
    $result = $this_hour.":".$this_minute;
} elseif($day >= 1) {
    $total_hour = ($day * 24) + $this_hour;
    $result = $total_hour.":".$this_minute;
}

echo $result;
//Outpput is = 72:00

i have tried the code above, but i want the result not to count holidays. Thanks.

2

Answers


  1. Here is example:

    date_default_timezone_set('Asia/Jakarta');
    $awal  = date_create("2023-08-18 00:00:00");
    $akhir = date_create("2023-08-21 00:00:00");
    
    // Array of holidays
    $holidays = array("2023-08-19"); // Example holiday. Add more dates as needed.
    
    $interval = DateInterval::createFromDateString('1 day');
    $periods = new DatePeriod($awal, $interval, $akhir);
    
    $holidayHours = 0;
    foreach ($periods as $period) {
        if (in_array($period->format('Y-m-d'), $holidays)) {
            $holidayHours += 24; // Exclude 24 hours for each holiday
        }
    }
    
    $diff  = date_diff($awal, $akhir);
    $hariNya = $diff->d;
    $jamNya = $diff->h;
    $menitNya = $diff->i;
    
    if($jamNya < 10){
        $jamNya2 = "0".$jamNya;
    } else {
        $jamNya2 = $jamNya;
    }
    
    if($menitNya < 10){
        $menitNya2 = "0".$menitNya;
    } else {
        $menitNya2 = $menitNya;
    }
    
    if($hariNya < 1){
        $durasi = $jamNya2.":".$menitNya2;
    } elseif($hariNya >= 1) {
        $gg = $hariNya * 24;
        $ff = $gg+$jamNya2 - $holidayHours; // Subtract holiday hours from total
        $durasi = $ff.":".$menitNya2;
    }
    
    echo $durasi;
    

    I have added this $holidays = array("2023-08-19"); for the example. You have to add your holiday days in this array.

    Login or Signup to reply.
  2. This a more correct edit.
    To calculate the difference in hours between two dates while excluding holidays, you can iterate through the dates between the start and end dates and skip counting the hours on holiday dates. Here’s the modified code:

    date_default_timezone_set('Asia/Jakarta');
    $start_date = date_create("2023-08-18 00:00:00");
    $end_date = date_create("2023-08-21 00:00:00");
    
    // this list of holidays is in Y-m-d format
    $holidays = array(
        '2023-08-19', // Example holiday
        // You can add more holidays as needed
    );
    
    // Calculate the total difference in hours excluding holidays
    $total_duration = 0;
    
    $current_date = clone $start_date;
    while ($current_date < $end_date) {
        $current_date_str = $current_date->format("Y-m-d");
    
        // Check if the current date is not aholiday
        if (!in_array($current_date_str, $holidays)) {
            $total_duration += 24; // Add 24 hours for each non-holiday day
        }
    
        $current_date->modify("+1 day");
    }
    
    $diff = date_diff($start_date, $end_date);
    $hour = $diff->h;
    $minute = $diff->i;
    
    $total_duration += $hour; // Add the remaining hours
    $this_hour = ($total_duration < 10) ? "0" . $total_duration : $total_duration;
    $this_minute = ($minute < 10) ? "0" . $minute : $minute;
    
    $result = $this_hour . ":" . $this_minute;
    echo $result;
    // Output is = 48:00
    

    In this code, I iterated through each day between the start and end dates. For each day, check if it’s not a holiday before adding 24 hours to the total duration. After iterating through all days, we add the remaining hours and minutes from the original calculation. The result will be the difference in hours between the two dates, excluding hours on holiday dates.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search