small loop, dont have time to code it.

Discussion in 'PHP' started by Talker, Dec 3, 2008.

  1. #1
    Hi,

    If someone could plz code this small php code for me.

    I have 4 variables to represent a startdate and enddate based on month and year.

    
    $startmonth = 3;
    $startyear = 2007;
    $endmonth = 5;
    $endyear = 2008;
    
    PHP:

    I want to loop through all months from 3-2007 to 5-2008 and do some processing for each month. Can anyone help me?
    Just make me the loop and ill code the process inside the loop myself.

    Thanks in advance.
     
    Talker, Dec 3, 2008 IP
  2. live-cms_com

    live-cms_com Notable Member

    Messages:
    3,128
    Likes Received:
    112
    Best Answers:
    0
    Trophy Points:
    205
    Digital Goods:
    1
    #2
    for ($x = $startmonth; $x <= $endmonth; $x++){

    You do it from here.
     
    live-cms_com, Dec 4, 2008 IP
  3. DomainCo.US

    DomainCo.US Well-Known Member

    Messages:
    2,124
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    100
    #3
    don't forget to include the year
    for ($i = $startyear ; $i <= $endyear; $i++){
    for ($x = $startmonth; $x <= $endmonth; $x++){
     
    DomainCo.US, Dec 4, 2008 IP
  4. Talker

    Talker Notable Member

    Messages:
    2,795
    Likes Received:
    108
    Best Answers:
    0
    Trophy Points:
    210
    #4
    yes, but we have a limit of month to be max 12.. so how do we go about ending the year at 12th month and then stating the new year at 1st month?
     
    Talker, Dec 4, 2008 IP
  5. Talker

    Talker Notable Member

    Messages:
    2,795
    Likes Received:
    108
    Best Answers:
    0
    Trophy Points:
    210
    #5
    I managed to write this:

    
    
    for ($i = $startyear ; $i <= $endyear; $i++){
       if($i < $endyear){$tendmonth=12;} else {$tendmonth=$endmonth;}
       if($completedyear == 1) {$tstartmonth=1;} else {$tstartmonth=$startmonth;}
       for ($x = $tstartmonth; $x <= $tendmonth; $x++){
          echo $i."-".$x."<br/>";
          $completedyear = 1;
       }
    }
    
    PHP:
    But then Mark Baker at phpfreaks helped me out and wrote this awesome code for me :)

    
    $startDate = mktime(0,0,0,$startmonth,1,$startyear);
    $endDate = mktime(0,0,0,$endmonth+1,1,$endyear);
    for($date = $startDate; $date < $endDate; $date = strtotime('+1 month',$date)) {
        echo 'Date is '.date('M-Y',$date).'<br />';
    }
    
    
    PHP:
    Great Stuff !
     
    Talker, Dec 4, 2008 IP
  6. DomainCo.US

    DomainCo.US Well-Known Member

    Messages:
    2,124
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    100
    #6
    nice fellow :)
     
    DomainCo.US, Dec 4, 2008 IP