increment date by week

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
assgar
Forum Commoner
Posts: 46
Joined: Fri Apr 20, 2007 9:00 pm

increment date by week

Post by assgar »

increment date by week


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

Code: Select all

 
<?
$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
?>
 
Scrumpy.Gums
Forum Commoner
Posts: 71
Joined: Thu Aug 30, 2007 2:57 pm
Location: Bristol, UK

Re: increment date by week

Post by Scrumpy.Gums »

You may want to have a look at date_modify. Which can then be used as follows :wink:

Code: Select all

$week_number = 1;
$week_cycle = 2;
$start_date = new DateTime("2008-02-01");
 
$nw = ($week_number * $week_cycle) + 1;
 
$start_date->modify("+$nw week");
$next_start = $start_date->format("Y-m-d");
 
$start_date->modify("+1 week");
$next_end = $start_date->format("Y-m-d");
 
$week_number = date("W", strtotime($next_start));
 
echo $next_start . '<br/>';
echo $next_end . '<br/>';
echo $week_number;
For me the output is:

Code: Select all

2008-02-22
2008-02-29
08
Post Reply