Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2004
    Posts
    82

    Unanswered: Months between two dates

    Hey guys,

    How can I take two dates (say 2005-12-29 and 2007-01-17)... and echo all the months between these dates?

    So:
    Code:
    January 2007
    December 2006
    November 2006
    October 2006
    September 2006
    August 2006
    July 2006
    June 2006
    May 2006
    April 2006
    March 2006
    February 2006
    January 2006
    December 2005
    If you get me?

    Thanks,
    Jordan

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    ..well I suppose you could write a function which returns the months as required
    it could return the values as an array if you wanted to use the returned values elsewhere in the script
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Aug 2004
    Posts
    82
    Ok... so I used the code:

    PHP Code:
    function get_months($date1$date2) {

    $time1 strtotime($date1);
    $time2 strtotime($date2);
    $my date('mY'$time2);
    $months = array(date('F'$time1));
    $f '';

    while(
    $time1 $time2) {
    $time1 strtotime((date('Y-m-d'$time1).' +15days'));

    if(
    date('F'$time1) != $f) {
    $f date('F'$time1);

    if(
    date('mY'$time1) != $my && ($time1 $time2))
    $months[] = date('F'$time1);
    }

    }

    $months[] = date('F'$time2);
    return 
    $months;
    }





    print(
    "<pre>");
    print_r(get_months('2005-02-20''2005-12-31'));
    print(
    "</pre>"); 

    Which echos:

    Code:
    Array
    (
        [0] => February
        [1] => March
        [2] => April
        [3] => May
        [4] => June
        [5] => July
        [6] => August
        [7] => September
        [8] => October
        [9] => November
        [10] => December
    )
    How can I get rid of everything but the actual months?

  4. #4
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    What do you mean "get rid of everything but the actual months"? The data is in whatever form your code was written to cause it to be in.

    Since you place the information into an array and return that array from the function, you have an array. You can then use a foreach(...) loop to iterate through each element in the array or instead of placing the information into an array, concatenate it into a string with the formatting you want in the function and return that string from the function.

  5. #5
    Join Date
    May 2007
    Posts
    4
    This should work...

    PHP Code:
    $data get_months('2005-02-20''2005-12-31');

    for(
    $i=0;$i<count($data);$i++){
     
    $month $data[$i];
     echo 
    "$month<br>";


  6. #6
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    If you want to make it even shorter :

    Code:
    $data = get_months('2005-02-20', '2005-12-31');
    
    for($i=0;$i<count($data);$i++){
     echo $data[$i]."<br>";
    }
    You don't even need the variable assignment. What a waste of memory!

  7. #7
    Join Date
    May 2007
    Posts
    4
    yeah every little helps

  8. #8
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    Shortest (tested) -
    PHP Code:
    foreach(get_months('2005-02-20''2005-12-31') as $value){
    echo 
    "$value<br />";

    Last edited by dbmab; 05-10-07 at 01:01.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •