increment date by week

Discussion in 'PHP' started by assgar, Jan 18, 2008.

  1. #1
    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
    ?>
    
    PHP:
     
    assgar, Jan 18, 2008 IP
  2. logondotinfo

    logondotinfo Peon

    Messages:
    314
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #2
    um, just on first glance, but you have mixed labels for what I assume are the same variables:

    $week number / $week cycle

    AND

    $week_number / $week_cycle

    that needs fixing for starters if it isnt a typo - if it is, sorry and forget i spoke :)
     
    logondotinfo, Jan 18, 2008 IP
  3. Gordaen

    Gordaen Peon

    Messages:
    277
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The second argument of date() and strtotime() functions must be a unix timestamp.

    <?
    $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", strtotime($start_date)));
    $next_end = date('Y-m-d',strtotime("+1 week", strtotime($next_start)))."<br>";
    $week_number = date('W', strtotime($next_start));//week number of the year
    ?>
    
    PHP:
     
    Gordaen, Jan 20, 2008 IP
  4. assgar

    assgar Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Good solution thanks.
     
    assgar, Jan 28, 2008 IP