Help Looping threw last 12 moths time()

Discussion in 'PHP' started by cornetofreak, Jan 17, 2009.

  1. #1
    hey ppl i got a problem!

    im trying to loop threw the last 12 months of this year from now and return the start of the current month in t he loop and the end of the current month in the loop if that makes send

    eg

    
    $months = array("last_month" => array($start_of_month => $end_of_month),
    "month_before_last" => array($start_of_month => $end_of_month)
    );
    
    PHP:
    the reason being is i have several records in my database with the UNIX TIMESTAMP / time() and want to get the count(id) for each month

    im trying to create a graph to show submissions for that month.

    hope someone can help me as im really rubbish at maths!

    Thanks in advanced!
     
    cornetofreak, Jan 17, 2009 IP
  2. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can do it by using mktime(). Just get the year and month from the timestamp and leave out the rest in mktime and it will give you the timestamp for the starting second of the month.
    To get the end second you can use strtotime() like this:
    
    $end_of_month = strtotime( '+1 month', $start_of_month ) - 1;
    [code]
    That should give you the last second of the month you are working on.
    
    I haven't tested any of this though. But it should work. And you should get around all those pesty months with more or less days than the rest of the months.
    Code (markup):
     
    tamen, Jan 18, 2009 IP
  3. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks dood, i see what you mean with strtotime() and ill go and test it now.

    thanks
     
    cornetofreak, Jan 18, 2009 IP
  4. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ok i think this could work but but theres another issue! take a look and see

    
    $months = array();
    $first_month = mktime(0, 0, 0, $i, 0, date("Y"));
    for($i=0;$i <= 12;$i++){
    	$first_month = mktime(0, 0, 0, $i, 0, date("y"));
    	$end_of_month = strtotime('+1 month', $first_month) - 1;
    	$months[$first_month] = $end_of_month;
    }
    foreach($months as $start => $end){
    	echo date("d-m-y",$start) . " ==> " . date("d-m-y",$end)."<br />"; 
    }
    
    PHP:
    ###########OUTPUT

    
    30-11-08 ==> 29-12-08
    31-12-08 ==> 30-01-09
    31-01-09 ==> 02-03-09
    28-02-09 ==> 27-03-09
    31-03-09 ==> 30-04-09
    30-04-09 ==> 29-05-09
    31-05-09 ==> 30-06-09
    30-06-09 ==> 29-07-09
    31-07-09 ==> 30-08-09
    31-08-09 ==> 30-09-09
    30-09-09 ==> 29-10-09
    31-10-09 ==> 30-11-09
    30-11-09 ==> 29-12-09
    
    Code (markup):
    its looping the months close enought but its ++ the year when with the date("Y")

    any ideas
     
    cornetofreak, Jan 18, 2009 IP
  5. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #5
    <?php
    
    $i = 0 and $times = array();
    while ( ++$i <= 12 )
        $times[$i] = strtotime(date('\1\s\t F Y ', strtotime( '-' . $i . ' months' )));
      
    /* Show it worked */ 
    foreach($times as $time)
    	echo date('jS F Y', $time) . ' - ' . $time . "<br />\n";
    PHP:
     
    Danltn, Jan 19, 2009 IP
  6. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    through, not threw ;-)
     
    Omzy, Jan 19, 2009 IP
  7. paitken

    paitken Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    There is SQL that can do this. You can use GROUP BY MONTH(field) WHERE MONTH(field) = Month#

    Might want to check out the mysql date/time functions.
     
    paitken, Jan 19, 2009 IP