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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
recci
Forum Commoner
Posts: 42
Joined: Tue Jul 29, 2008 10:01 pm

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

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

antorizz
Forum Newbie
Posts: 8
Joined: Sat Nov 03, 2012 12:03 pm

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

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

format() can grab the number of days in the month too.
Post Reply