Hi, here I included my code to find no of working days between two years.

But in leap year I do not know how to find the working days.
Can anyone help me?



function calculate($month,$year)
{
$Start_Date="1/".$month."/".$year."";
$myTime = strtotime($Start_Date);  // Use whatever date format you want
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); // 31
//$workDays = 0;
while($daysInMonth > 0)
     {
        $day = date("D", $myTime); // Sun - Sat
        if($day != "Sun" && $day != "Sat")
            $workDays++;
        $daysInMonth--;
        $myTime += 86400; // 86,400 seconds = 24 hrs.

    }
return $workDays;

}



<?php echo calculate($MONTH,$YEAR)?>

In my knowledge the cal_days_in_month function already takes into account leap years. I think the error is in line 10:

$Start_Date="1/".$month."/".$year."";

To make string representation correct it should be in a format that represents a unique date (i.e. American month, day and year - mm "/" dd "/" y):

$Start_Date=$month."/"."1/".$year."";
// or in more readable form
$Start_Date = "$month/1/$year";

And this is my version of the calculate function:

function calculate($month, $year) {
    $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    $workDays = 0;
    for($d = 1; $d <= $daysInMonth; $d++) {
        $dateString = "$month/$d/$year";
        $currentDayTS = strtotime($dateString);
        $day = date("D", $currentDayTS); // Sun - Sat
        if($day != "Sun" && $day != "Sat") {
            $workDays++;
        }
    }
    return $workDays;
}
Member Avatar for diafol

OK ANtony. I'm assuing that we're going to have a series of these threads, one function at a time. This is the third in a series. Here's a rough and ready class. SImilar to posts already given and to broj1's above

class weekDays
{
    public function month($month, $year)
    {
        $dt1 = new DateTime($year.'-'.$month.'-01');
        $lastDay = $dt1->format('t');
        $weekdays=20;
        for($d=29;$d<=$lastDay;$d++) {
            $wd = $dt1->modify($year . '-' . $month . '-' . $d)->format('w');
            if($wd > 0 && $wd < 6) $weekdays++;
        }
        return $weekdays;
    }

    public function year($year)
    {
        $dt1 = new DateTime($year . '-01-01');
        $startDay = ($dt1->format('L') == 1) ? 30 : 31;
        $weekdays = 260;
        for($d=$startDay;$d<=31;$d++) {
            $wd = $dt1->modify($year . '-12-' . $d)->format('w');
            if($wd > 0 && $wd < 6) $weekdays++;
        }
        return $weekdays;
    }

    public function between($fromDate, $toDate)
    {
        $dt1 = new DateTime($fromDate);
        $dt2 = new DateTime($toDate);
        $days = (int) $dt1->diff($dt2)->format('%a') + 1;
        $weekdays = floor($days/7)*5;
        $extras = $days%7;
        $dt2->sub(new DateInterval('P'.$extras.'D'));
        for($d=0;$d<$extras;$d++) {
            $wd = $dt2->add(new DateInterval('P1D'))->format('w');
            if($wd > 0 && $wd < 6) $weekdays++;
        }
        return $weekdays;
    }

}

USAGE EXAMPLES:

$WD = new weekDays;

echo "YEARS<br />";
for($i=2000;$i<2030;$i++)
    echo $i . ': ' . $WD->year($i) . '<br />';

echo "<br />MONTHS<br />";
for($i=1;$i<13;$i++)
    echo $i . ', 2015: ' . $WD->month($i,2015) . '<br />';

echo "<br />BETWEEN<br />";
for($i=1;$i<31;$i++)
    echo '2015-10-01 - 2015-10-' . $i . ': ' . $WD->between('2015-10-01','2015-10-' . $i) . '<br />';

If you know how to calculate number of working days in a year. They you just need to determine if the year of February that occurs in your span is a leap year. If it is a leap year determine if the day of the week of feb 29 of that year is a week day. Leap year is calculated as follows.

  1. If the year is evenly divisible by 4, go to step 2. else go to step 5
  2. If the year is evenly divisible by 100, go to step 3. else go to step 5
  3. If the year is evenly divisible by 400, go to step 4. else go to step 5
  4. The year is a leap year (it has 366 days). Stop
  5. The year is not a leap year (it has 365 days). Stop
Member Avatar for diafol

No need to do that the L format in daterime will tell you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.