Values not generating for last month in datetime php

I am trying to get the number of sat&sun for each month between two given dates.

eg: For start_date as 5-Jul-2015 and end_date as 29-Aug-2015 I need to get 7 sat&sun in Jul and 9 in Aug.

So far I am getting values till the second last month ie: not getting Aug.
What to do?


$countSat = 0;
$countSun = 0;
$start = new DateTime('05-07-2015');
$month = $start->format('n');
$end = new DateTime('29-08-2015');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);

$i = 0;
foreach ($period as $dt) {
    if ($month != $dt->format('n')) {
        $results[$i] = $countSun + $countSat;
        $countSun = 0;
        $countSat = 0;

    if ($dt->format('N') == 7) {
    if ($dt->format('N') == 6) {
    $month = $dt->format('n');
echo '<pre>';

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s