Link to home
Start Free TrialLog in
Avatar of 3rdLifeWebDev
3rdLifeWebDev

asked on

Counting Specific "Day of the Week"s left in a month.

I need a php script that will count how many specific days of the week are left in a Month.

Eg. Today is Saturday, Sept 17. There are 2 Sundays left in this month.

I need to be able to count depending on specific days left.
ASKER CERTIFIED SOLUTION
Avatar of acbxyz
acbxyz
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You may be able to do this easily if you understand how PHP handles DATETIME information.  Look at the "fifth Friday" among the practical applications in this article.  If that does not make it immediately clear to you, please post back here with a clarifying question.

https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Best regards, ~Ray
Avatar of jastacdoss
jastacdoss

Try this:

function getDaysLeft($thisDate, $dNum) {
    // DEFINE NAMES
    $wkNames = Array(0=>"Sunday", 1=>"Monday", 2=>"Tuesday", 3=>"Wednesday", 4=>"Thursday", 5=>"Friday", 6=>"Saturday");
    $countMonth = 0;
    
    // SPLIT DATE
    $sDate = explode("/",$thisDate);
    $m = $sDate[0];
    $d = $sDate[1];
    $y = $sDate[2];
        
    // LOOP THROUGH REST OF MONTH AND RETURN DAYS
    while ($m == date("n",mktime(0,0,0,$m,$d,$y))) {
        $newDate = date("w",mktime(0,0,0,$m,$d,$y));
        if ($newDate == $dNum && isset($isDay)) { $countMonth++; echo $d . "|" . $m . "<BR>"; $d = $d + 7;}
        else { $d++; $isDay = 0; }
    }
    
    return "There are " . $countMonth . " " . $wkNames[$dNum] . "'s left in " . date('F',mktime(0,0,0,$m,$d,$y));
}

echo getDaysLeft("8/31/2011", 4);

Open in new window