title: Calculate number of weeks based on dates using PHP
body: <p>I would like to be able to output a timeline containing the number of weeks from a series of dates contained in a table. So for example, let's say I've got the dates 12/9/10 (Thursday), 12/13/10 (Monday), 12/15/10 (Wednesday), and 12/21/10 (Tuesday), each in a record in a MySQL table.</p>
  
  <p>What I would want outputted is something that calculates the number of weeks based on those dates, so like:</p>
  
  <p>Week 1: 12/9/10
  Week 2: 12/13/10, 12/15/10
  Week 3: 12/21/10</p>
  
  <p>I know how to get the week number in an given year (so like today we're in week 49) but since I could have ANY date range, I'm calculating the weeks, not the week of the year.</p>
  
  <p>I could simply convert weeks of the year to a count and display them sequentially (for if the dates start in week 49 and go through week 52, week 49 = 1, week 50 = 2, etc.) but if I have dates that span over 2 years (like 12/25/10 through 1/2/11) that's problematic.</p>
  
  <p>Any help would be greatly appreciated! I don't need the MySQL code--just the date string conversion. I've been spinning my wheels on this one!</p>
  
  <p>UPDATE: Just thought I'd share the code that finally solved this. This isn't my final solution as the data still needs to be massaged, but I got what I wanted and it's data I can now work with. Thanks to everyone who posted an answer.</p>
  
  <pre><code>&lt;?php 
  header("Content-type: text/html; charset=utf-8");
  require_once('includes/connections/know_db.php');
  ?&gt;
  
  &lt;?php
  
  //First let's get all the years any given project will span...
  mysql_select_db($database_know_db, $know_db);
  $query_GetYears = sprintf("SELECT DISTINCT(YEAR(target_date)) as project_years FROM project_items WHERE projects_id = 136 AND target_date IS NOT NULL ORDER BY project_years ASC");
  $GetYears = mysql_query($query_GetYears, $know_db) or die(mysql_error());
  
  //A function allowing us to extract the week of the year from the next query, and then convert its value into an integer.
  function ConvertToWeek($target_date) {
      $week = date('W', strtotime($target_date));
      $week_int = intval($week);
      return $week_int;
  }
  
  //Now let's loop through our years, and get project item data (via MySQL) for each...
  while ($row_GetYears = mysql_fetch_assoc($GetYears)) {
      echo $row_GetYears['project_years']."&lt;br /&gt;";
  
      mysql_select_db($database_know_db, $know_db);
      $query_GetItems = sprintf("SELECT DISTINCT(target_date) FROM project_items WHERE projects_id = 136 AND target_date IS NOT NULL AND YEAR(target_date) = '".$row_GetYears['project_years']."' ORDER BY target_date ASC");
      $GetItems = mysql_query($query_GetItems, $know_db) or die(mysql_error());
  
      //Loop through the results of our project items, convert them to week numbers from our function, then toss them into an array.
      while ($row_GetItems = mysql_fetch_assoc($GetItems)) {
          $weeks[] = ConvertToWeek($row_GetItems['target_date']);
          //Array_unique essentially removes duplicate numbers...
          $result = array_unique($weeks); 
      }
  
      // get the first value in the $weeks array, then to be safe, convert its value to an integer.
      $start_week = array_shift(array_values($weeks));
      $start_week_no = intval($start_week);
  
      // get the last value in the $weeks array (will use this later to find differences in weeks between overlapping years).
      $end_week = array_pop(array_values($weeks));
  
          echo 'Start week: '.$start_week_no."&lt;br /&gt;";
          echo 'End week: '.$end_week."&lt;br /&gt;";
  
      //Output our weeks for the purposes of display.
      foreach ($result as $week_count) {
          echo ltrim($week_count, "0").'&lt;br /&gt;';
      }
  
      /*Now let's find the weeks in the sequence where weeks are not represented (missing).
      By doing this, we can get a number of for each week we don't have where datasets will be empty.
      */
  
      // construct a new array:1,2....max(given array).
      $result2 = range($start_week_no,max($result));                                                    
  
      // use array_diff to get the missing weeks 
      $missing = array_diff($result2,$result);
  
      //Output our missing weeks for the purposes of display.
      foreach ($missing as $missing_weeks) {
              echo $missing_weeks.' (missing)&lt;br /&gt;';
      }
  
      /*
      Before we close our original while loop--the one that loops through each year, we need to unset our arrays so they are empty upon 
      each new loop.
      */
      unset($weeks);
      unset($result);
  
  //End our original while loop.
  }
  ?&gt;
  </code></pre>