Add the correct number of days for each month to this function
Results 1 to 6 of 6

Thread: Add the correct number of days for each month to this function

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3

    Add the correct number of days for each month to this function

    Using the following function I can display the years and months between two dates, but how can I add the correct days for each month as another array within each month? I can't just add the days manually as I need it to account for leap years etc.

    PHP Code:
    function yearMonth($start_date$end_date)
    {
        
    $begin = new DateTime$start_date );
        
    $end = new DateTime$end_date);
        
    $interval = new DateInterval('P1M'); // 1 month interval

        
    $period = new DatePeriod($begin$interval$end);

        foreach ( 
    $period as $dt )
            
    $years[$dt->format"Y" )][] = $dt->format"F" );

        return 
    $years;
    }

    $list  =  yearMonth("2007-03-24""2009-06-26");
    var_dump($list); 

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,432
    From the manual page for date(), note that the "t" format string gives you the number of days in a month. If you wanted an array of the days, then you could use range() to create an array from 1 to that number.

  3. #3
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,906
    The t format gives the number of days in the month containing the date/time; L indicates leap years.
    Last edited by Weedpacket; 10-08-2013 at 11:42 PM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    It was an answer more like this I was looking for:

    PHP Code:
    function yearMonth($start_date$end_date)
    {
        
    $begin = new DateTime$start_date );
        
    $end = new DateTime$end_date);
        
    $interval = new DateInterval('P1D'); // 1 month interval

        
    $period = new DatePeriod($begin$interval$end);

        
    $lastMonth null;
        
    $lastYear null;
        
    $aResult = array();
        foreach ( 
    $period as $dt )
        {
            if (
    $dt->format('Y') != $lastYear)
            {
                
    $lastYear $dt->format('Y');
            }
            if (
    $dt->format('F') != $lastMonth)
            {
                
    $lastMonth $dt->format('F');
            }
            if (!isset(
    $aResult[$lastYear]))
            {
                
    $aResult[$lastYear] = array();
            }
            if (!isset(
    $aResult[$lastYear][$lastMonth]))
            {
                
    $aResult[$lastYear][$lastMonth] = array();
            }
            
    $aResult[$lastYear][$lastMonth][] = $dt->format('d');
        }

        return 
    $aResult;


  5. #5
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,432
    In other words, you wanted a less efficient way of going about the answer that you received instead:

    PHP Code:
    function yearMonth($start_date$end_date

        
    $begin = new DateTime$start_date ); 
        
    $end = new DateTime$end_date); 
        
    $interval = new DateInterval('P1D'); // 1 month interval 

        
    $period = new DatePeriod($begin$interval$end); 

        
    $aResult = array(); 
        foreach ( 
    $period as $dt 
        {
            
    $aResult[$dt->format('Y')][$dt->format('F')][] = $dt->format('d');
        } 

        return 
    $aResult

    EDIT: Woops, guess it's a bit more than that since you wanted a partial return for the months at the start and end of the range. Okay...

    EDIT2: Was over thinking it... and got fooled by the incorrect "1 month interval" code comment. Corrected code snippet above.
    Last edited by bradgrafelman; 10-08-2013 at 06:28 PM.

  6. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks that is a more efficient way. I now just need to echo this out into a grid as I am trying to create a Gantt chart layout with years as top row then months then days. Is there a better way of going about that?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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