0
HI,
 I want to calculate number of working days for the academic year between two dates like(01-06-2015 to 31-10-2015). 
I saw all codes that are doing  only for curtrent year.
 If I want to do future years , how to do automatically,
 Here is my  code what I tried , It works for only one year, I need to calculate dynamically for future years

$beginday = date("Y-m-01");
$lastday  = date("Y-m-t");

$nr_work_days = getWorkingDays($beginday, $lastday);
echo $nr_work_days;

function getWorkingDays($startDate, $endDate)
{
    $begin = strtotime($startDate);
    $end   = strtotime($endDate);
    if ($begin > $end) {
        echo "startdate is in the future! <br />";

        return 0;
    } else {
        $no_days  = 0;
        $weekends = 0;
        while ($begin <= $end) {
            $no_days++; // no of days in the given interval
            $what_day = date("N", $begin);
            if ($what_day > 5) { // 6 and 7 are weekend days
                $weekends++;
            };
            $begin += 86400; // +1 day
        };
        $working_days = $no_days - $weekends;

        return $working_days;
    }
}
2
Contributors
6
Replies
18
Views
1 Year
Discussion Span
Last Post by diafol
0

Ok, Don't use the seconds thing to calculate as it will mess up on daylight saving and leap years.
So you want all days minus all sats and suns?

Many ways to do this.This is my first shot:

function getNumWeekDays($from,$to)
{
    $dt1 = new DateTime($from);
    $dt2 = new DateTime($to);
    $interval = $dt1->diff($dt2);
    $days = $interval->days + 1; //to include last date itself
    $weekDays = floor($days / 7) * 5;
    $extras = $days % 7;
    if($extras !=0) $extras--;
    return $weekDays + $extras;
}

$date1 = '2014-01-01';
$date2 = '2015-10-10';

print_r (getNumWeekDays($date1,$date2));
0

one time it shows output like ù and next time it shows like 4297, when page refresh.
for the given input:
$date1 = '2014-01-01';
$date2 = '2015-10-10';

0

I changed to input like
$date1 = '2015-01-01';
$date2 = '2015-10-10'; That time also itshows the same output ù and 4297
I can not understand this. Can you explain it?

0

Which version of php are you using?

The manual states:

days
If the DateInterval object was created by DateTime::diff(), then this is the total number of days between the start and end dates. Otherwise, days will be FALSE.
Before PHP 5.4.20/5.5.4 instead of FALSE you will receive -99999 upon accessing the property.

The function works perfectly for me. Show your code.

Edited by diafol

0

Well you're buggered then. You'll need an alternative method. Either that or enter the modern age :) . Your version is almost 5 years old. Currently we're at 5.6.15. Consider upgrading?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.