Hi


I am trying to increment the date using weekly increments.
I need to determine the start and end dates and the week number of the year.

If you can suggest another approache that is OK, but this is what I came up with.

I am having problems getting the correct increment and week number of the year.

result received:
(week number) 01-1
(next start) 1970-01-07
(next end) 1970-01-07

result expected:
(week number) 02
(next start) 2008-02-14
(next end) 2008-02-21

<?
$week number = 1;//the for loop increment number 
$week cycle = 2;//This will provide the date for the 1st, 2nd, 3rd or 4th week of the month.
$start_date = "2008-02-01";


$nw = ($week number * $week cycle) + 1;//+ 1 to be in the next range	

$next_start = date("Y-m-d",strtotime("+$nw week", $start_date));
$next_end = date('Y-m-d',strtotime("+1 week", $next_start))."<br>";
$week_number = date('W', $next_start);//week number of the year
?>

Most of your code seems to be fine, but I'm a little confused as to what the $week_cycle is supposed to do.

I took your code and worked out the example script (at the bottom of this post).

It starts with a beginning date - I entered the first day (Monday) of the first week of this year (12/31/2007). Remember that Week #1 will usually start in the previous year (unless the first day of the year is Monday).

It then loops through and saves the date of the Monday of each week ($week_data) and the date of the Sunday ($week_data).

I also didn't understand why you added a full week to get the week's end. Doesn't the week end on Sunday (6 days)? If you wanted it to be Monday to Monday, you could easily switch the ("+6 Days") back to ("+1 Week").

Script loops through 53 times, creating an entry for each week and saving it in an array indexed by the Week No.

Then you can simply loop through the array to output each week (like I did), or look up a specific week by it's Week No and echo that.

It seems to me this does what you need, unless I misunderstood what you were looking for...

Good luck,
- Walkere

<?php

$increment = 1;		//  # of weeks to increment by

//  First day of the first week of the year
$startdate = strtotime("31 December 2007");

//  $all_weeks[1] is the first partial week of the year
//  $all_weeks[53] is the last partial week of the year
$all_weeks = array();

for ($week = 0; $week <= 52; $week += $increment)
{
  $week_data = array();

  $week_data['start'] = strtotime("+$week weeks", $startdate);
  $week_data['end'] = strtotime("+6 days", $week_data['start']);

  $all_weeks[$week + 1] = $week_data;
}

echo "<pre>";
echo "Week No.	Start Date	End Date\r\n";

foreach ($all_weeks as $week => $week_data)
{
  echo $week . "\t\t" . date("Y-m-d", $week_data['start']) . 
	"\t" . date("Y-m-d", $week_data['end']) . "\r\n";
}

echo "</pre>"

?>

Hi

After some time I think I have done it.
Let nme know if you have any suggestions how to refine the code.

The result below is for a three week (7 day) cycle for the month of January.
Note: week days Monday (550) to Friday (554)

Result Columns:
date range(start date, end date), day of week, time range(start time and end time)

inner loop)1
2008-01-01 2008-01-07 550 09:00:00 12:00:00
2008-01-22 2008-01-28 550 09:00:00 12:00:00
2008-01-01 2008-01-07 551 09:00:00 12:00:00
2008-01-22 2008-01-28 551 09:00:00 12:00:00
2008-01-01 2008-01-07 552 09:00:00 12:00:00
2008-01-22 2008-01-28 552 09:00:00 12:00:00
2008-01-01 2008-01-07 553 09:00:00 12:00:00
2008-01-22 2008-01-28 553 09:00:00 12:00:00
2008-01-01 2008-01-07 554 09:00:00 12:00:00
2008-01-22 2008-01-28 554 09:00:00 12:00:00

(inner loop)2
2008-01-08 2008-01-14 550 10:00:00 13:00:00
2008-01-29 2008-01-31 550 10:00:00 13:00:00
2008-01-08 2008-01-14 551 10:00:00 13:00:00
2008-01-29 2008-01-31 551 10:00:00 13:00:00
2008-01-08 2008-01-14 552 10:00:00 13:00:00
2008-01-29 2008-01-31 552 10:00:00 13:00:00
2008-01-08 2008-01-14 553 10:00:00 13:00:00
2008-01-29 2008-01-31 553 10:00:00 13:00:00
2008-01-08 2008-01-14 554 10:00:00 13:00:00
2008-01-29 2008-01-31 554 10:00:00 13:00:00

(inner loop)3
2008-01-15 2008-01-21 550 16:00:00 20:00:00
2008-01-15 2008-01-21 551 16:00:00 20:00:00
2008-01-15 2008-01-21 552 16:00:00 20:00:00
2008-01-15 2008-01-21 553 16:00:00 20:00:00
2008-01-15 2008-01-21 554 16:00:00 20:00:00

<?

$max_week = '3';
$start_date = '2008-01-01';
$end_date = '2008-01-31';

/**-------------loop through number of weeks------------**/
for($i = 1; $i <= $max_week; $i++)
   {
       	//format to two characters
	$week_num = "0$i";
   
       	//detemine start date interval 
	 if($i == 1)
       	   {
       		$start_date = $start_date;//week 1
	   }
	  else
	     {
        	//week 2, 3 and 4	
		$wk_start = strtotime($start_date);
		$start_date = date("Y-m-d", strtotime("+7 days", $wk_start));
	     }
         
         
	   //flush previous array contents
	   unset ($group_seg);
	
	   /**------------get group module data to apply to schedule------------**/
	   $query = "SELECT distinct(s.seg_id) w.group_id,
	             	    s.time_from, s.time_to, w.weekday
		     FROM group_week w, day_segment s
		     WHERE s.model_id = w.model_id
		     AND w.group_id = '$group_id'";
  
	   $result = mysqli_query ($mysqli, $query);
	   while($row = mysqli_fetch_array($result))
		{
 		   $group_seg[] = $row;
		}


    	   /**------------------- event type info to insert-----------------------**/ 
  	   //loop through segment start and end time
	   foreach($group_seg as $group_segment)
		{
	 	   //database stored time from daily model segments
		   $start_time = $group_segment['time_from'];
		   $end_time = $group_segment['time_to'];
		   $group_id = $group_segment['group_id'];
		   $day = $group_segment['weekday'];

		   //more than one week cycle used interval date
		   if($max_week > 1)
			{

			     //determine date start incrementation using max_week
			    switch($max_week)
    			    	{
            			   case '1': //1 week
            			      $cycle_days = 7;
             			   break;
           		    	   case '2': //2 weeks
           		    	      $cycle_days = 14;
              			   break;
           			   case '3': //3 weeks
           			       $cycle_days = 21;
             			   break;
           			   case '4': //4 weeks
           			       $cycle_days = 28;
            			   break;
            			}

                             /**----------increment using $cycle_days from above--------**/      
                             for($f = $start_date; $f <= $end_date; $f = date("Y-m-d", strtotime($f . "+ $cycle_days day")))
		                {
			   	   //set start date
  			   	   $startdate = strtotime($f);
   			           $type_start_date = date("Y-m-d", $startdate);

			           //set end date with addtional 6 days
			          $wk_start = strtotime($type_start_date);
		     	          $wk_end_date = date("Y-m-d", strtotime("+6 days", $wk_start));
					     					
			          //check incremented end date does not exceed selected end date
			          if($wk_end_date <= $end_date)	
			   	     {
				        $type_end_date = $wk_end_date;//incremented end date
				     }
				    else
				       {
					   $type_end_date = $end_date;//selected end date
				       }
										
	
                                   /**INSERT STATEMENT GOES HERE**/									}
			 }
			 else
			    {
				// single week cycle insert selected start and end dates no manipulation needed
				$type_start_date = $start_date;
				$type_end_date = $end_date;
								
		  		/**INSERT STATEMENT GOES HERE**/
			    }
		     }//foreach	
	    	}//for
	    	
    ?>
This question has already been answered. Start a new discussion instead.