PHP date function not work properly

Hey there again!

I have this PHP function that calculates working days between two dates
example: 2016-10-20 TO 2016-10-31 (should be 8 days but it calculates just 7)

my function:

 function number_of_working_days($startDate, $endDate)
{
    $workingDays = 0;
    $startTimestamp = strtotime($startDate);
    $endTimestamp = strtotime($endDate);
    for ($i = $startTimestamp; $i <= $endTimestamp; $i = $i + (60 * 60 * 24)) {
        if (date("N", $i) <= 5) $workingDays = $workingDays + 1;
    }
	
    return $workingDays;
}

Whats wrong?Thanks for replays!
Edit: i just tried to do calculation inserting in $endDate date from 2016-10-20 TO 2016-10-31 .
for all days it was correct exept the last date of the month 2016-10-31 , its like it ignors last day of the month

make your data more visible:

you may want to use the DatePeriod or DateTime class with modify() instead of using unix timestamps.

1 Like

Why does it not count last day as 31st?

did you ever hear that clocks are set forward/backward exactly one hour twice a year? it’s called DTS - daylight saving time. DTS may be available in your country :smiley: check out your local documentation for “october 31 2016”.

1 Like

Yes it is :blush: next one is next week :smile:

Look at the link he provided, it is because it isn’t showing 2016-10-31 for the final date, it has 2016-10-30 23:00 for some odd reason.

Something is wrong with your calculation, though I’m not 100% certain what.

You will notice the second line of output, which is the $endTimestamp, and the last output of $i do not match. They are off by an hour.

I’d encourage you to go about this using a different technique, such as…

Yeah chorn mentioned that!Now will need to try modify it somehow!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.