Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 112

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 112

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 112

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 112
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3368: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3370: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3371: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3372: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
OrbWorks Community Forum • View topic - Best way to count weekend days
Page 1 of 1

PostPosted: Fri Nov 03, 2006 12:33 pm
by sangahm
I am trying to find a faster way to count weekend days between two dates. My code for this is:

Date StartDate, EndDate; // two dates to calculate # of days
int LoopCounter;
int IntDay;

StartDate = Fdate; //used for this internal method
EndDate = Sdate;

IntDay = Fdate.weekday; // 0=Sunday

while (LoopCounter > 0) {
if ((IntDay == 0) || (IntDay == 6)) {
WEDays = WEDays + 1;
} //end if

This works fine for two dates that are within a year from each other, but when I have two dates that are several years about, it takes up to 20 seconds on my T3.

The internal function for counting the total number of days between two dates is relatively instantaneous. Can I build a similar routine that is just as fast?

PostPosted: Fri Nov 03, 2006 2:12 pm
by mlambrigger
Try this, I have tested for 2 consecutive date (FDate must be the first). I have not trried to invert the two date, but you can add test and invert function if you want.
Code: Select all
Date StartDate, EndDate; // two dates to calculate # of days
  int WEDays = 0;
//  Fdate; // first date in your code
//  Sdate; // Second date
  WEDays = ((SDate.diffdays(FDate)-(SDate.weekday-FDate.weekday))/7)*2;
  if(SDate.weekday == 6) WEDays++;
  if(FDate.weekday == 0) WEDays++;


I think it will be the fastest way to calculate your weekend days.

Marc Lambrigger

PostPosted: Fri Nov 03, 2006 3:56 pm
by sangahm
Outstanding Marc!! That is drastically more efficient and solves my problem.

PostPosted: Sun Dec 17, 2006 8:29 pm
by sangahm
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by mlambrigger</i>
<br />
WEDays = ((SDate.diffdays(FDate)-(SDate.weekday-FDate.weekday))/7)*2;
if(SDate.weekday == 6) WEDays++;
if(FDate.weekday == 0) WEDays++;
[/code]
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">

Marc-

I'm doing some debugging work around counting/not counting weekend days & such, and I thought I understand your algorithm, but it turns out I really don't. Can you tell me what the expression, <b>(SDate.weekday-FDate.weekday)</b> is supposed to do and why it's subtracted from diffdays? I assume that it has to do with including the start and end date in the calculation, but it doesn't seem to be getting me the expected results.

PostPosted: Mon Dec 18, 2006 12:37 pm
by mlambrigger
If you made your calculation on a period begginig on a wednesday and ending on another wednesdy, it will not be usefull, but if you do on a period beginning on a monday and ending on a thursday, this will substract the difference between thos different days in the week to have a multiple of 7 to make the integer division.
Example: from a monday to a tuesday, the difference between those date will be 7*w+1 where w is a number of complet week.
As the function weekday return 0 for sunday, 1 for mondays, ..., the (SDate.weekday-FDate.weekday) will return 1
tuesday - monday = 2 - 1 = 1. So the complet expression will be:

Code: Select all
((SDate.diffdays(FDate)-(SDate.weekday-FDate.weekday))/7)*2=
(((7*w+1)-(tuesday - monday))/7)*2=
((7*w+1-(2-1))/7)*2=
((7*w+1-1)/7)*2 =
((7*w)/7)*2=
(w)*2 = 2w


Second example:
starting on a sunday, ending on a friday:
Code: Select all
((SDate.diffdays(FDate)-(SDate.weekday-FDate.weekday))/7)*2=
(((7*w+5)-(friday-sunday))/7)*2=
((7*w+5-(5-0))/7)*2=
((7*w+5-5)/7)*2 = 2w

The problem is that we do not have count the first sunday, that is why we have this line:
Code: Select all
if(FDate.weekday == 0) WEDays++;

and so the result will be: 2w + 1.

This is the same problem if we have the second date as a saturday

I see yet that there will be a mistake if we begin with a saturday and finish with another saturday: in so the two if must be rewrite as this:
Code: Select all
  if(SDate.weekday != FDate.weekday){
    if(SDate.weekday == 6) WEDays++;
    if(FDate.weekday == 0) WEDays++;
  }

I hope this will help you and resolve your problem.

Marc Lambrigger

PostPosted: Mon Dec 18, 2006 3:33 pm
by sangahm
Yes, that was my suspicion. When I tracked through some scenarios, I was seeing differences depending on the start and end dates.

I need to think through your response a little more, but it does seem to make sense. I think I now have enough to make it work.

Thanks again!