| | |
increment date by week
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 82
Reputation:
Solved Threads: 0
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
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
PHP Syntax (Toggle Plain Text)
<? $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 ?>
•
•
Join Date: Jan 2008
Posts: 57
Reputation:
Solved Threads: 5
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
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
PHP Syntax (Toggle Plain Text)
<?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>" ?>
•
•
Join Date: Oct 2006
Posts: 82
Reputation:
Solved Threads: 0
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
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
PHP Syntax (Toggle Plain Text)
<? $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 ?>
Last edited by assgar; Feb 9th, 2008 at 4:19 pm.
![]() |
Similar Threads
- Week Scheduler in C#.. (C#)
- looping to insert event types into schedule (PHP)
- Schedule Repeat events (PHP)
- Another C++ N00B Begging For Help (C++)
- MyPimpedSpace & Hott-Spot (Websites for Sale)
Other Threads in the PHP Forum
- Previous Thread: php validator
- Next Thread: develop website by using free templet from provider
| Thread Tools | Search this Thread |
# 5.2.10 alexa apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date directory display dissertation dropdown dynamic echo echo$_get[x]changingitintovariable... email encode error fairness file files folder form forms function functions google href htaccess html image images include indentedsubcategory insert ip javascript joomla legislation limit link local login mail memberships menu mlm multiple multipletables mysql mysqlquery newsletters oop open paypal pdf persist php problem provider query radio random recursion remote rss script search server sessions sms sockets source space spam sql syntax system table tutorial update upload url validator variable video web youtube





