How to produce list of months between two dates?

Discussion in 'PHP' started by KingCobra, Sep 11, 2011.

  1. #1
    Dear friends,
    I need to produce list of months between two dates. It was not so hard by adding "+1 month" technology.
    But the problem is that some months have 30 days and some 31 days. And February have 28 or 29 days.
    So I am requesting for help from you. See the image for output. You just need output like table 1. Other tables only for show you example.
    Note that in output I shows the count duration for more than 1 yeas (that goes year 2012) just for showing you February month.

    The following 3 value will come from user input.

    $countType = "monthly"; // It can be (monthly, quarterly)
    $annualEndDate = "12/31/2010"; // It can be (10/10/2010, 09/28/2011 etc.)
    $countStartDate = "05/31/2011"; // It can be (10/12/2010, 01/01/2011 etc.)

    (As annualEndDate is "12/31/2010" so the new annualStartDate will be "01/01/2011" (next day). you have to determine it. And the calculation will be for one year based on new annualStartDate)

    OUTPUT
    May 31, 2011
    June 30, 2011
    July 31, 2011
    August 31, 2011
    September 30, 2011
    October 31, 2011
    November 30, 2011
    December 31, 2011

    See this image and ignore attachment image that showing at the bottom.
    month-list.jpg

    You can see my another post here if it can little help you:
    http://forums.digitalpoint.com/showthread.php?t=2276972

    Thanks
     

    Attached Files:

    Last edited: Sep 11, 2011
    KingCobra, Sep 11, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    The last day of any month is
    
    
    function lastday($month, $year) {
        $result = strtotime("{$year}-{$month}-01");
        $result = strtotime('-1 second', strtotime('+1 month', $result));
        return date('Y-m-d', $result);
     }
    
    PHP:
    As you say, the rest is just the "+ 1 month" technoliogy. You could just loop within the function - give it the start and end dates and pull the date of the end of month for each month.
     
    Rukbat, Sep 11, 2011 IP
  3. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Dear Rukbat,

    Thank you for your help. Would you please write the code for following message-
    "As you say, the rest is just the "+ 1 month" technoliogy. You could just loop within the function - give it the start and end dates and pull the date of the end of month for each month. "




    $yearStart = "07/01/2011"
    $countStart = "11/28/2011";
    $countEnd = "04/15/2012";

    This type of 1 month difference between two dates ($countStart & $countEnd) is very easy with "+1 month" function.

    November 28, 2011
    December 28, 2011
    January 28, 20112
    February 28, 2012
    March 28, 2012


    But I cannot do this type of difference if $countStart= "11/30/2011" or "11/31/2011"

    November 30, 2011
    December 31, 2011
    January 31, 20112
    February 29 , 2012 // As leap year. otherwise February 28, 2012
    March 31, 2012
     
    KingCobra, Sep 12, 2011 IP