Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2011-09-17
3
311 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 109

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

839 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