Page 1 of 1

PHP-General - Add the correct number of days for each month

Posted: Mon Oct 07, 2013 5:48 pm
by recci
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. I also need it to tell me what the day is as IN thu, Fri, sat etc..

Code: Select all

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);  


Re: PHP-General - Add the correct number of days for each mo

Posted: Fri Oct 11, 2013 10:04 am
by antorizz
Try searching through the PHP.net manul, a quick search pointed me to this function,

Code: Select all

cal_days_in_month();
Although I can't answer your question, you might be able to figure it out from there.
http://us1.php.net/manual/en/function.c ... -month.php

Re: PHP-General - Add the correct number of days for each mo

Posted: Fri Oct 11, 2013 12:52 pm
by requinix
format() can grab the number of days in the month too.