increment date by week

Thread Solved

Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

increment date by week

 
0
  #1
Jan 18th, 2008
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

  1. <?
  2. $week number = 1;//the for loop increment number
  3. $week cycle = 2;//This will provide the date for the 1st, 2nd, 3rd or 4th week of the month.
  4. $start_date = "2008-02-01";
  5.  
  6.  
  7. $nw = ($week number * $week cycle) + 1;//+ 1 to be in the next range
  8.  
  9. $next_start = date("Y-m-d",strtotime("+$nw week", $start_date));
  10. $next_end = date('Y-m-d',strtotime("+1 week", $next_start))."<br>";
  11. $week_number = date('W', $next_start);//week number of the year
  12. ?>
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 57
Reputation: Walkere is an unknown quantity at this point 
Solved Threads: 5
Walkere Walkere is offline Offline
Junior Poster in Training

Re: increment date by week

 
0
  #2
Jan 18th, 2008
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['start']) and the date of the Sunday ($week_data['end']).

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

  1. <?php
  2.  
  3. $increment = 1; // # of weeks to increment by
  4.  
  5. // First day of the first week of the year
  6. $startdate = strtotime("31 December 2007");
  7.  
  8. // $all_weeks[1] is the first partial week of the year
  9. // $all_weeks[53] is the last partial week of the year
  10. $all_weeks = array();
  11.  
  12. for ($week = 0; $week <= 52; $week += $increment)
  13. {
  14. $week_data = array();
  15.  
  16. $week_data['start'] = strtotime("+$week weeks", $startdate);
  17. $week_data['end'] = strtotime("+6 days", $week_data['start']);
  18.  
  19. $all_weeks[$week + 1] = $week_data;
  20. }
  21.  
  22. echo "<pre>";
  23. echo "Week No. Start Date End Date\r\n";
  24.  
  25. foreach ($all_weeks as $week => $week_data)
  26. {
  27. echo $week . "\t\t" . date("Y-m-d", $week_data['start']) .
  28. "\t" . date("Y-m-d", $week_data['end']) . "\r\n";
  29. }
  30.  
  31. echo "</pre>"
  32.  
  33. ?>
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Re: increment date by week

 
0
  #3
Jan 28th, 2008
Thanks great solution.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Re: increment date by week

 
0
  #4
Feb 9th, 2008
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


  1.  
  2. <?
  3.  
  4. $max_week = '3';
  5. $start_date = '2008-01-01';
  6. $end_date = '2008-01-31';
  7.  
  8. /**-------------loop through number of weeks------------**/
  9. for($i = 1; $i <= $max_week; $i++)
  10. {
  11. //format to two characters
  12. $week_num = "0$i";
  13.  
  14. //detemine start date interval
  15. if($i == 1)
  16. {
  17. $start_date = $start_date;//week 1
  18. }
  19. else
  20. {
  21. //week 2, 3 and 4
  22. $wk_start = strtotime($start_date);
  23. $start_date = date("Y-m-d", strtotime("+7 days", $wk_start));
  24. }
  25.  
  26.  
  27. //flush previous array contents
  28. unset ($group_seg);
  29.  
  30. /**------------get group module data to apply to schedule------------**/
  31. $query = "SELECT distinct(s.seg_id) w.group_id,
  32. s.time_from, s.time_to, w.weekday
  33. FROM group_week w, day_segment s
  34. WHERE s.model_id = w.model_id
  35. AND w.group_id = '$group_id'";
  36.  
  37. $result = mysqli_query ($mysqli, $query);
  38. while($row = mysqli_fetch_array($result))
  39. {
  40. $group_seg[] = $row;
  41. }
  42.  
  43.  
  44. /**------------------- event type info to insert-----------------------**/
  45. //loop through segment start and end time
  46. foreach($group_seg as $group_segment)
  47. {
  48. //database stored time from daily model segments
  49. $start_time = $group_segment['time_from'];
  50. $end_time = $group_segment['time_to'];
  51. $group_id = $group_segment['group_id'];
  52. $day = $group_segment['weekday'];
  53.  
  54. //more than one week cycle used interval date
  55. if($max_week > 1)
  56. {
  57.  
  58. //determine date start incrementation using max_week
  59. switch($max_week)
  60. {
  61. case '1': //1 week
  62. $cycle_days = 7;
  63. break;
  64. case '2': //2 weeks
  65. $cycle_days = 14;
  66. break;
  67. case '3': //3 weeks
  68. $cycle_days = 21;
  69. break;
  70. case '4': //4 weeks
  71. $cycle_days = 28;
  72. break;
  73. }
  74.  
  75. /**----------increment using $cycle_days from above--------**/
  76. for($f = $start_date; $f <= $end_date; $f = date("Y-m-d", strtotime($f . "+ $cycle_days day")))
  77. {
  78. //set start date
  79. $startdate = strtotime($f);
  80. $type_start_date = date("Y-m-d", $startdate);
  81.  
  82. //set end date with addtional 6 days
  83. $wk_start = strtotime($type_start_date);
  84. $wk_end_date = date("Y-m-d", strtotime("+6 days", $wk_start));
  85.  
  86. //check incremented end date does not exceed selected end date
  87. if($wk_end_date <= $end_date)
  88. {
  89. $type_end_date = $wk_end_date;//incremented end date
  90. }
  91. else
  92. {
  93. $type_end_date = $end_date;//selected end date
  94. }
  95.  
  96.  
  97. /**INSERT STATEMENT GOES HERE**/ }
  98. }
  99. else
  100. {
  101. // single week cycle insert selected start and end dates no manipulation needed
  102. $type_start_date = $start_date;
  103. $type_end_date = $end_date;
  104.  
  105. /**INSERT STATEMENT GOES HERE**/
  106. }
  107. }//foreach
  108. }//for
  109.  
  110. ?>
Last edited by assgar; Feb 9th, 2008 at 4:19 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC