SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

    PHP Code:
    // 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>" "\n";
      while (
    $FullWeek 365)
      {
        
    $StartDate strtotime("Monday this week -$FullWeek days");
        
    $StartWeek date('W (m-d-Y)'$StartDate);
        
    $WorkWeeks .= "<option value='" $StartWeek "'>" $StartWeek "</option>" "\n";
        
    $FullWeek $FullWeek 7;
      }
      return 
    $WorkWeeks;

    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,122
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Maybe someone decided that week 0 did not make sense, so started it at 1.

    Or, maybe Java weeks are not 0 based?

  4. #4
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,809
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,809
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by GHicks View Post
    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.

    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •