Solved

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

Posted on 2011-09-17
3
307 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
ID: 36555026
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
ID: 36555185
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
ID: 36557789
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

929 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

12 Experts available now in Live!

Get 1:1 Help Now