Week #'s don't match PHP to Java

I am rewriting a database front end that was previously done in Java. I’ve created the below function to return 52 weeks of work week values for the user to select a specific work week to request a time sheet for (I am aware it won’t fully function as I will be returning too much data in my option values, but that is an easy fix, just haven’t done it yet.

My dilemma (and this may be a moot point after I discuss it with the client tomorrow, but I’d still like an answer just for my own well being. Is that the code the previous person used is returning week #46 (11/05/12) where my code is returning that same week as week #45 with the same base date.

Hopefully someone might have a suggestion for me to explain the discrepancy between the week number variance?


// Build a list of the past 52 work weeks (Monday is basis)
function GetWorkWeeks()
{
  $FullWeek = 7;
  $StartDate = strtotime('Monday this week');
  $StartWeek = date('W (m-d-Y)', $StartDate);
  $WorkWeeks = "<option value='" . $StartWeek . "' selected>" . $StartWeek . "</option>" . "\
";
  while ($FullWeek < 365)
  {
    $StartDate = strtotime("Monday this week -$FullWeek days");
    $StartWeek = date('W (m-d-Y)', $StartDate);
    $WorkWeeks .= "<option value='" . $StartWeek . "'>" . $StartWeek . "</option>" . "\
";
    $FullWeek = $FullWeek + 7;
  }
  return $WorkWeeks;
}

Oddly enough, my calendar agrees with PHP, it says 11/5 is Week #45, not #46. Then I went to http://www.calendar-365.com/week-number.html and it agrees with PHP too. So in my opinion the Java application was wrong.

Maybe someone decided that week 0 did not make sense, so started it at 1.

Or, maybe Java weeks are not 0 based?

Well, after looking some more through client documentation it appears that any year that starts on Thursday or later the numbering is off as Week 1 on their list ends up being just Thursday, Friday, Saturday. If the year starts on Wednesday or earlier then both Java and PHP agree (or rather his Java code, can’t say it is a “java” thing without knowing more about it).
Thanks for the input, I’m going to contact the client later today and discuss with the HR person (who this would affect the most) to see if this is even an issue to worry about. I guess I could construct more code to determine the first day of the year and increment by 1 the years that are appropriate if need be.

Greg

The ISO standard week (which is what PHP returns when you specify W in the format) defines week 1 as the week commencing on a Monday that includes the first Thurshay of the year. So if the 1st Jan is a Thursday then week 1 starts on 29th December of the prior year. If the 1st January is a Friday then week 1 starts on 4th January.

Sounds like your Java code wasn’t following the standards.

No, but after having a rather lengthy discussion with the client I have found that I also need to skew my results off the standard. So whether the first week of the year is simply a single day, or a full week, it is week 1. Now to figure out exactly what triggers this so I can define it appropriately.

Basically, their “work week” begins on Monday. So all employee shifts through Sunday would be included in the previous work week. Some years they do match the “standard” work weeks, and it appears to be those years that have the first day of the year before Thursday. Any years that start Thursday-Sunday I will need to increment the week number by 1. But JUST for that calendar year. Since their dropdown is inclusive for 24 months, I will also need to re-adjust when the while loop reaches a new year.

Hopefully I’m missing something simple here, but it sounds like I’ll be scratching my head over code for a bit.

Greg

Get the day of the week for the 1st January and if it is 0, 5, or 6 then add one to the week number.

So a query to what day Jan. 1 is would return a numerical value, with 5,6,0 being Friday, Saturday, Sunday respectively? I’ll have to check if Thursday being the first causes the same discrepancy and then just add 4 to that variable I would assume?

Thanks for the tip, I’ll look into it some more later tonight when I get back to coding.

Greg