title: Start date and End date of two weeks within a date range in PHP
body: <p>I want to get the start date  and end date of two weeks within a date range in PHP.  </p>
  
  <p>Week start = Sunday and week end = Saturday</p>
  
  <p>ie)for example if date range is </p>
  
  <blockquote>
    <p>$start = '2013-01-01'  and<br>
     $end ='2013-02-28'</p>
  </blockquote>
  
  <p><strong>Required Result</strong></p>
  
  <blockquote>
    <p>Start Date-->2013-01-01 End Date -->2013-01-12<br>
    Start Date-->2013-01-13 End Date -->2013-01-26<br>
    Start Date-->2013-01-27 End Date -->2013-02-09<br>
    Start Date-->2013-02-10 End Date -->2013-02-23<br>
    Start Date-->2013-02-24 End Date -->2013-02-28  </p>
  </blockquote>
  
  <p><strong>Code</strong>  </p>
  
  <pre><code>  &lt;?php
  $st = '2013-01-01';
  $et ='2013-02-28';
  $start_date = date('Y-m-d', strtotime($st));
  $end_date = date('Y-m-d', strtotime($et));
  $end_date1 = date('Y-m-d', strtotime($et. '+ 6 days'));
  
  $weekfrom = array();
  $weekto = array();
  
  for($date = $start_date; $date &lt;= $end_date1; $date = date('Y-m-d', strtotime($date. ' + 14 days')))
  {
  
      $week =  date('W', strtotime($date));
      $year =  date('Y', strtotime($date));
      $from = date("Y-m-d", strtotime("{$year}-W{$week}+1")); //Returns the date of monday in week
      if($from &lt; $start_date) $from = $start_date;
      $to = date("Y-m-d", strtotime("{$year}-W{$week}-6"));   //Returns the date of sunday in week
      if($to &gt; $end_date)
       {
          $to = $end_date;       
  
       }
      if($from &lt; $to)
      {
      array_push($weekfrom,$from);
      array_push($weekto,$to);
      }
  
  }
  $n = count($weekfrom);
  
  for($i = 0;$i&lt;$n;$i++)
  {
    echo "Start Date--&gt;".$weekfrom[$i];
    echo " End Date --&gt;".$weekto[$i]."\n";
  }
  
  
  ?&gt;
  </code></pre>
  
  <p><strong>Current Result</strong></p>
  
  <blockquote>
    <p>Start Date-->2013-01-01 End Date -->2013-01-05<br>
    Start Date-->2013-01-13 End Date -->2013-01-19<br>
    Start Date-->2013-01-27 End Date -->2013-02-02<br>
    Start Date-->2013-02-10 End Date -->2013-02-16<br>
    Start Date-->2013-02-24 End Date -->2013-02-28   </p>
  </blockquote>
  
  <p><a href="http://codepad.org/H0vIdNn0" rel="nofollow"><strong>DEMO</strong></a></p>
  
  <p>In the current result the start date is correct.But the issue is within the end date. Help me to find the issue</p>