Solved

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

Posted on 2011-09-17
3
303 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:3rdLifeWebDev
3 Comments
 
LVL 10

Accepted Solution

by:
acbxyz earned 500 total points
Comment Utility
This object oriented programming needs at least PHP 5.2. An example how to use it in the attached file.
function date_dow_left($dow)
{
    $datenow = new DateTime();
    $dateadd = clone $datenow;
    // if you want to skip today:
    $dateadd->add(new DateInterval('P1D'));

    $return = 0;
    while ($datenow->format('m') == $dateadd->format('m')) {
        if ($dateadd->format('N') == $dow) {
            $return++;
            $dateadd->add(new DateInterval('P1W'));
        } else {
            $dateadd->add(new DateInterval('P1D'));
        }
    }
    return $return;
}

Open in new window

daysleft.php
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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.

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

Best regards, ~Ray
0
 
LVL 1

Expert Comment

by:jastacdoss
Comment Utility
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

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now