Getting Mondays date from a week number doesn't work for 2009?

Discussion in 'Web Design and Development' started by big_malk, Dec 19, 2008.

  1. big_malk macrumors 6502a

    Joined:
    Aug 7, 2005
    Location:
    Scotland
    #1
    I'm making a website, part which needs saves sales records for people each day. The client wants a page which shows the sales weekly in a table, so each row should start on Monday, and show each days sales for the week.

    I used the code below to take a year and week number, and show the dates for Monday and Sunday, and it worked fine for December 08, but come 2009 it's just plain wrong.

    2008
    Week 51 - 15th Dec - 21st Dec
    Week 52 - 22nd Dec - 28th Dec

    2009
    Week 1 - 3rd Jan - 9th Jan
    Week 2 - 10th Jan - 16th Jan

    15th and 22nd December are Mondays by my calendar, but the 3rd of January is a Saturday? I just don't get how this could work in December but not in January?
    Anyone see something I can't? This has been driving me mad!

    PHP:
    $time strtotime("4th January ".$year ' +' . ($week 1) . ' weeks');
    $mondayTime strtotime('-' . (date('w'$time) - 1) . ' days'$time);
                    
    $from date('jS M'$mondayTime);
    $to date('jS M'strtotime('+ 6 days'$mondayTime));
     
  2. big_malk thread starter macrumors 6502a

    Joined:
    Aug 7, 2005
    Location:
    Scotland
    #2
    I got a email with a reply to this thread but it isn't showing on this page :S

    That was my first thought, but it works fine for week 1 2008, the first day of which is 31th Dec '07!

    And I don't see why it would pick the 3rd which is a Saturday anyway :confused:
     
  3. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #3
    Based on your code I put together the following PHP.
    PHP:
    $time strtotime("4th January 2009 +52 weeks");
    $mondayTime strtotime('-'. (date('w'$time)-1) .' days'$time);

    $from date('jS M'$mondayTime);
    $to date('jS M'strtotime('+ 6 days'$mondayTime));
    echo 
    $from ." - "$to;
    This outputs:
    Code:
    4th Jan - 10th Jan
    for me on my own machine. My only guess is that perhaps the servers time zone might be confused about something. Tested with PHP 5.2.5.
     
  4. big_malk thread starter macrumors 6502a

    Joined:
    Aug 7, 2005
    Location:
    Scotland
    #4
    I get the 3rd Jan in $from on a few servers I've tried it on, I don't think the timezone would interfere as there's no mention of hours? But even if it did put things of by a few hours, the date it should give is 29th December!
     
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    The timezone idea was just me thinking aloud. You might want to check out the comments at the strtotime manual page. Look for the comment by Beat on Oct 1 2007. He points out an issue that he ran into, and the timezone made a difference if you look at the results. I don't know that the issue mentioned there is the same as yours, but it's worth a look, and might lead you in the right direction.

    Also, as a note about my previous code, it actually looks at 2010 rather than 2009 because I added 52 weeks to the 2009 date. I was paying a ton of attention when I through it together and wasn't sure what values the OP had for their variables.
     
  6. big_malk thread starter macrumors 6502a

    Joined:
    Aug 7, 2005
    Location:
    Scotland
    #6
    Thanks for the advice about reading those comments. Unfortunately, his workaround won't for me as you can't use week numbers in mktime(), and my clients require me to use their Yahoo! hosting, which uses an old version (4) of PHP.
    Although, I never thought to look for known bugs in the old version of PHP until now, maybe I should try that, since I can't find a problem in my code :(
     
  7. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #7
    This will work for any date range, PHP4 or 5 and will format properly for the current locale settings:


    PHP:

    // Get year and week number (ISO-8601) for right now, but could be
    // any date converted to a timestamp and passed as 2nd argument
    // in each date function:

    $year=date("Y"); 
    $week=date("W");

    // Pass year and week to our function which takes into consideration
    // the first week situation.  Returned is an array storing Mon-Sun week day info:

    $dayTimes getDaysInWeek($week$year);

    // Loop through our array and display formatted week day info:

    foreach ($dayTimes as $dayTime) {
      echo (
    strftime('%A %B %d, %Y'$dayTime) . "<br />");
    }


    // Our function that does the bullwork

    function getDaysInWeek ($weekNumber$year) {
      
    // Count from '0104' because January 4th is always in week 1
      // (according to ISO 8601).
      
    $time strtotime($year '0104 +' . ($weekNumber 1)
                        . 
    ' weeks');
      
    // Get the time of the first day of the week
      
    $mondayTime strtotime('-' . (date('w'$time) - 1) . ' days',
                              
    $time);
      
    // Get the times of days 0 -> 6
      
    $dayTimes = array ();
      for (
    $i 0$i 7; ++$i) {
        
    $dayTimes[] = strtotime('+' $i ' days'$mondayTime);
      }
      
    // Return timestamps for mon-sun.
      
    return $dayTimes;
    }

    The example uses the current date but obviously you can feed the function any week and year. The output when I ran it today (19th) is:

    Monday December 15, 2008
    Tuesday December 16, 2008
    Wednesday December 17, 2008
    Thursday December 18, 2008
    Friday December 19, 2008
    Saturday December 20, 2008
    Sunday December 21, 2008

    -jim
     

Share This Page