I am trying to convert a timestamp in the format "yyyy-mm-dd" to an integer fiscal week. Currently, my algorithm is 4(k-1) + floor(d/7)+1 where k is the integer month and d is the integer day of the month. Saturday starts the new fiscal week.
This has some flaws and is incorrect. For instance consider Saturday, January 28th 2012:
- My algorithm computes 5(which is correct).
Next consider, Friday February 3rd:
- My algorithm computes 5(which is correct).
Now consider Saturday February 4th.
- My algorithm computes 5(this is incorrect).
It appears my algorithm will always fail in between months and thus accumulate an increasing error.
How can I compute the correct fiscal week?
Compute the number of days elapsed before the beginning of the current month, add it to dd and then divide by 7. Finally, add 1 to the resulting number.
So for Feb 4th, the answer would be (31 + 4)/7 + 1 = 6.