Ok, I am close to bald from pulling out my hair on this one. I want to show the seven days, starting with monday, for a particular calendar week in a year.

For current week (49) in 2003
For week 2 in 2004
For week x in 2005

I can get from a date to the calendar week but no clue how to do the reverse. I have searched and found nothing. I am hoping that someone here has done something like this or can point me in the right direction. I would post code but at this point it is just mushy spaghetti and doesn`t make sense to me anymore.

Thanks,
Sonya

    I think strtotime() will be your friend:

    echo date('m-d-Y', strtotime('49 weeks', strtotime('2003-1-1')));
    

    Results in: 12-10-2003

    What's happening:

    strtotime() with only '49 weeks' takes the current time (today) and addes 49 weeks to it and gives you a date in 2004. I was looking at your post and it didn't look like you wanted that.

    strtotime() has a 2nd parameter which is the time offset specified as a timestamp. So the second parameter is strtotime('2003-1-1') which takes the start of this year and produces a timestamp which the other strtotime() likes. If you wanted to find the 49th week of any year, just swap out the year in '2003-1-1' - if you wanted to get really fancy, you could swap out '2003-1-1' with: date('Y-1-1') and have code you wouldn't have to ever touch. It might look like:

    echo date('m-d-Y', strtotime('49 weeks', strtotime(date('Y-1-1'))));
    

    On second thought... You might want to put the base year of the calendar you're working with in for the 2nd parameter as seen above. As in, just putting in the current year isn't going to be overly helpful to you. But I included it as an example.

    I'm just using 'm-d-Y' as the date() parameter. You should be able to tweak this to whatever format date() likes...

      One little addition - January, 1st is not always falls on Monday.
      As you need print week starting from Monday then you need:

      $year = 2003;
      $week = 49;
      
      // get the timestamp for jan 1st + number of weeks of specified year
      $start_date = strtotime($week." weeks 1/1/".$year);
      
      // 0 - Sunday, 1 - Monday, ..., 6 - Saturday
      $day_of_week = date('w',$start_date);
      
      // 1 - Monday, ..., 7 - Sunday
      if( $day_of_week==0 )
      	$day_of_week = 7;
      
      // finding timestamp of Monday in a week
      $start_date = $start_date - 24*60*60*($day_of_week-1);
      
      for( $i=0; $i<7; $i++ ) {
      	echo date('r',$start_date+24*60*60*$i)."<br>\n";
      }

        The first week of the year (as returned by date('W')) is the one which contains the first Thursday of the year - its Monday might be in December the previous year (2004 is an example; the Monday for its first week is 2003-12-29, not 2004-01-05).

        $year = 2003;
        $week = 49;
        $Jan1 = mktime(1, 1, 1, 1, 1, $year);
        $weekdayJan1 = date('w', $Jan1);
        $Monday = strtotime(($week-1) . ' weeks '.((4-$weekdayJan1)%7-3) . ' days', $Jan1);
        echo date('Y m d',$Monday);
        

          WOW, you guys ROCK! Now my problem is the weeks in the next yr. and previous yr. I have toyed with putting a variable in the URL that passes the values.

          Problem: it is january 2004 and the user goes to previous weeks - needs to recognize the year change (going back and forth between weeks)

          or

          it is December 2003 and the user goes to future weeks - needs to recognize the year change (going back and forth between weeks).

          Thanks for any help or guidance,
          Sonya

          <?php 
          if (!$showkw){$week = date("W");} else {$week = $showkw;}
          
          $year = date("Y");
          $pyr = 0;
          $cyr = date("Y");
          if (!empty($nyr))
          {
          $nyr = $nyr;
          $year = $nyr;
          }
          else 
          {
          $nyr = 0;
          }
          
          if (!empty($pyr))
          {
          $pyr = $pyrr;
          $year = $pyr;
          }
          else 
          {
          $pyr = 0;
          }
          
          if ($week == 52)
          {
          $prev_week = $week - 1;
          $next_week = 1;
          $nyr = date("Y") + 1;
          }
          elseif ($week == 1)
          {
          $prev_week = 52;
          $next_week = $week + 1;
          $pyr = date("Y") - 1;
          }
          else
          {
          $prev_week = $week - 1;
          $next_week = $week + 1;
          }
          
          
          // get the timestamp for jan 1st + number of weeks of specified year
          $start_date = strtotime(($week-1)." weeks 1/1/".$year);
          
          // 0 - Sunday, 1 - Monday, ..., 6 - Saturday
          $day_of_week = date('w',$start_date);
          
          // 1 - Monday, ..., 7 - Sunday
          if( $day_of_week==0 )
              $day_of_week = 7;
          
          // finding timestamp of Monday in a week
          $start_date = $start_date - 24*60*60*($day_of_week-1);
          
          echo "<table border=\"1\">";
          if (!empty($nyr))
          {
          echo "<tr><td><a href=\"" . $PHP_SELF . "?pyr=" . $pyr . "&nyr=" . $nyr . "&showkw=" . $prev_week . "\"><small><<</small></a></td><td colspan=\"5\" align=\"CENTER\"><b>KW $week </b></td>	<td><a href=\""  . $PHP_SELF . "?nyr=" . $nyr . "&showkw=" . $next_week . "\"><small>>></small></a></td></tr>";
          }
          else 
          {
          echo "<tr><td><a href=\"" . $PHP_SELF . "?pyr=" . $pyr . "&showkw=" . $prev_week . "\"><small><<</small></a></td><td colspan=\"5\" align=\"CENTER\"><b>KW $week </b></td>	<td><a href=\""  . $PHP_SELF . "?nyr=" . $nyr . "&showkw=" . $next_week . "\"><small>>></small></a></td></tr>";
          }
          
          echo "<tr>";
          for( $i=0; $i<7; $i++ ) {
          //    echo date('r',$start_date+24*60*60*$i)."<br>\n";
          			$display_date = date('d.m',$start_date+24*60*60*$i); 
          			echo "<td>$display_date</td>";
          }
          echo "</tr>";
          echo "<tr><td align=\"center\" colspan=\"7\"><small><a href=\"" . $PHP_SELF . "?showkw=" . $currKW . "&user_id=" .  $user_id . "\">Current Week</a></small></td></tr>";
          echo "</table>";
           ?>
          

            I skipped over your code, but I'm thinking strtotime() will help.

            strtotime('-1 week', $UserSelectedDate);

            With this, You can subtract one week from whatever date and PHP will take care of worrying if its in 2003 or 2004.

              4 days later

              Unfortunately, that did not work, I have struggled with the whole "how to deal with the first few and last few weeks of a year (knowing which year being the problem here) when the user goes back and forth between weeks" issue for days now to no avail.

              Any thoughts out there? The code is in a post above.

              Thanks,
              Sonya

                6 days later

                Finally, this accounts for calendar week, year, whether it is a leap year, how many calendar weeks are in the year (and previous year for going backwards.)

                Hope this helps someone out. Code critiques are welcome!

                <?php 
                
                if (empty($yr)) //passed through links
                {
                 	  $uYr = date("Y");
                }
                else
                {
                 	  $uYr = $yr;
                } 
                //checks how many calendar weeks in a year
                function weeknumb_check($uYr)
                {
                    $jan1 = getdate( mktime( 0, 0, 0, 1, 1, $uYr ) );
                    //check if year is a leap-year
                    if (date("L", mktime(0,0,0,1,1,$uYr)))
                    {
                    	$s = 1; //yes it is a leap year
                    }
                    else 
                    {
                      $s = 0;
                    }
                    //check how many weeks the year has
                    if ( ( $jan1['wday'] == 3 && $s == 1 ) || $jan1['wday'] == 4 ) 
                    {
                     	return $weeks = 53;
                    } 
                    else
                    {
                      return $weeks = 52;
                    }	
                }//end of function
                
                if (!empty($showkw))//passed through links
                {
                 	  $week = $showkw;
                }
                else
                {
                 		$week = date("W");
                }
                //set weeks for this year
                $weeks = weeknumb_check($uYr);
                
                if ($showkw == $weeks)//set values for kw 1 and next year
                {
                 	 	$uYear = $uYear + 1 ;
                		$nWeek = 1; 
                }
                else
                {
                 		$nWeek = $week + 1; 
                }
                
                if ($showkw == 1)//set values for previous year
                {
                		$pWeek = $weeks; 
                }
                else
                {
                 		$pWeek = $week - 1; 
                }
                
                
                
                
                $jan1_day_of_week = date('w',$jan1);
                $start_date = strtotime(($week-1)." weeks 1/1/".$uYr);
                // 0 - Sunday, 1 - Monday, ..., 6 - Saturday
                $day_of_week = date('w',$start_date);
                // 1 - Monday, ..., 7 - Sunday
                if( $day_of_week==0 )
                    $day_of_week = 7;
                
                // finding timestamp of Monday 
                if ($day_of_week > 4)//if jan 1 is on a fri, sat or sun than it is in last year's calendar weeks
                {
                $start_date = $start_date - (24*60*60*($day_of_week-1)) + (24*60*60*7);
                }
                else
                {
                $start_date = $start_date - 24*60*60*($day_of_week-1);
                }
                $Month = date('M',$start_date+24*60*60*$i);
                $Year = date('Y',$start_date+24*60*60*$i);
                echo "<table border=\"1\">";
                if ($showkw == 1)
                {
                 	 	//check if previous year is 53 or 52 weeks
                		$weeks = weeknumb_check($uYr - 1);
                		$pWeek = $weeks;
                		echo "<tr><td><a href=\"" . $PHP_SELF . "?yr=" . ($uYr - 1) . "&showkw=" . $pWeek . "\"><small><<</small></a></td><td colspan=\"5\" align=\"CENTER\"><b>$Month $Year KW $week </b></td><td><a href=\"" . $PHP_SELF . "?yr=" . $uYr . "&showkw=" . $nWeek . "\"><small>>></small></a></td></tr><BR>"; 
                }
                elseif ($showkw == $weeks)
                {
                 		$pWeek = $week - 1;
                		echo "<tr><td><a href=\"" . $PHP_SELF . "?yr=" . $uYr . "&showkw=" . $pWeek . "\"><small><<</small></a></td><td colspan=\"5\" align=\"CENTER\"><b>$Month $Year KW $week </b></td><td><a href=\"" . $PHP_SELF . "?yr=" . ($uYr + 1) . "&showkw=" . $nWeek . "\"><small>>></small></a></td></tr><BR>"; 
                }
                else 
                {
                echo "<tr><td><a href=\"" . $PHP_SELF . "?yr=" . $uYr . "&showkw=" . $pWeek . "\"><small><<</small></a></td><td colspan=\"5\" align=\"CENTER\"><b>$Month $Year KW $week </b></td><td><a href=\"" . $PHP_SELF . "?yr=" . $uYr . "&showkw=" . $nWeek . "\"><small>>></small></a></td></tr><BR>";
                }
                
                echo "<tr><td>Mon</td><td>Tues</td><td>Weds</td><td>Thurs</td><td>Fri</td><td>Sat</td><td>Sun</td></tr>";
                
                echo "<tr>";
                for( $i=0; $i<7; $i++ ) {
                			$display_date = date('d.m',$start_date+24*60*60*$i); 
                			//$dispDay = date('d',$start_date+24*60*60*$i);
                			//$dispMonth = date('m',$start_date+24*60*60*$i);
                			echo "<td>$display_date</td>";
                			}
                ?>
                
                -Sonya
                
                  Write a Reply...