Skip to main content

Calculate Number of working days in a month excluding weekends(Saturday and Sunday) using PHP

The below function is used to calculate number of working days in a month.

Function Calling:
$no_days=rns_countDays($row['date'],array(0, 6));


Function:
function rns_countDays($date, $ignore) { 
    //echo countDays(array(0, 6), start_date, end_date);
    $count = 0;

    $ndays=0;
    $now=date("Y-m-d");
    $month=date("m",strtotime($date));
    $year=date("y",strtotime($date));   
   
    if($month==date("m") && $year==date('y')) { //Date diff month start date to current date
        $from_date = new DateTime($year.'-'.$month.'-01');
        $to_date = new DateTime($now);

        $days_diff=$from_date->diff($to_date); //echo $days_diff->d;
        $ndays=$days_diff->format("%a");
        for($i=0;$i<$ndays;$i++){
            $modif = $from_date->modify('+1 day');
            $weekday = $from_date->format('w');
            if(in_array($weekday, $ignore) == false) { $count++; }
        }
    } else {   
        $counter = mktime(0, 0, 0, $month, 1, $year);
        while (date("n", $counter) == $month) {
            if (in_array(date("w", $counter), $ignore) == false) {
                $count++;         
            }
            $counter = strtotime("+1 day", $counter);
        }
    }
    return $count;
}

Comments

  1. Table games like black jack aren't almost as lucrative – to the on line casino – as slots. Earn benefits like Free SlotPlay®, discounts for dining and accomodations, comps, entry into promotions, unique invites to special events, and far more. Play a machine that has gone a 카지노사이트.online long time|a very long time} without paying off — it is because of|as a outcome of} of} hit.

    ReplyDelete

Post a Comment