?
Solved

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

Posted on 2011-09-17
3
Medium Priority
?
320 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
[X]
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
3 Comments
 
LVL 10

Accepted Solution

by:
acbxyz earned 2000 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 111

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

766 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