Solved

How do I figure out every 3rd Thursday in a month?

Posted on 2003-12-04
4
368 Views
Last Modified: 2008-03-06
I am still working on my calendar and now I need to be able to schedule every 3rd something...
I would like to have 2 drop downs --
One that says every, every other, every First, every 2nd, every 3rd every 4th, every Last. and then one that says day,Monday,Tuesday,....Friday.

I am entering each event into a table and have it working where it will put every single date for a range of dates or every "Thursday" for example and I can get "every other" Thursday - just can't figure out how to determine the second thurday in a month.

Thanks
james

Here is part of the script.....

$range = range (0, $distance);

foreach ($range as $value) {
        $Rangetimestamps = mktime(0,0,0,$Startmonth,($Startday+$value),$Startyear);
        $RangeDates = date("mdY", $Rangetimestamps);
                $RangeDayNumber = date("d", $Rangetimestamps);
                $RangeDayText=  date("D", $Rangetimestamps);
                $RangeMonths = date("m", $Rangetimestamps);

If ($RangeDayText == "Thu") {
        echo $RangeDayText." Text";
        echo $RangeDates."  ".$RangeDayNumber."<p>";
}

}

Here is what outputs if I enter 12/4/2003 for a start date and 1/1/2004 for and enddate and select Thursday...

Thu Text12042003 04

Thu Text12112003 11

Thu Text12182003 18

Thu Text12252003 25

Thu Text01012004 01


Thu Text
0
Comment
Question by:cubbyboy57
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:petoskey-001
ID: 9876397
The simplest method would be to use your existing script.  Modify it like this...

If ($RangeDayText == "Thu") {
      $first = 0;
      $last = 0;
      if ($every2nd == TRUE) {
             $first = 8;
             $last = 14;
      }
      if ($every3rd == TRUE) {
             $first = 15;
             $last = 21;
      }
      if ($every4th == TRUE) {
             $first = 22;
             $last = 28;
      }
      if ($everyLast == TRUE {
             $last = date("t", $Rangetimestamps);
             $first = $last - 6; // Yes 6 because we include first and last days in the check
      }
      if ($first > 0) {
            if (($RangeDayNumber >= ($first + date("d", $Rangetimestamps)) and
                ($RangeDayNumber <= ($last + date("d", $Rangetimestamps))) {
                    echo $RangeDayText." Text";
                   echo $RangeDates."  ".$RangeDayNumber."<p>";
            }
      } else {
      echo $RangeDayText." Text";
      echo $RangeDates."  ".$RangeDayNumber."<p>";
}

0
 

Author Comment

by:cubbyboy57
ID: 9879053
Thanks for the reply, I am having trouble following the statement (there is a syntax error or something) If ($everyLast == TRUE {  I added the ")" but now I get a parse error unexpected "{" at line 88.  I thought it was the number of ( )'s but I have tried almost every combo and can't seem to get anything to work.

This is line 87-89
 if ($first > 0) {
            if (($RangeDayNumber >= ($first + date("d", $Rangetimestamps)) and
                ($RangeDayNumber <= ($last + date("d", $Rangetimestamps))) {

Can you help me figure out the problem?

Here is the revised script....

//
// loop through the days
$range = range (0, $distance);
foreach ($range as $value) {
        $Rangetimestamps = mktime(0,0,0,$Startmonth,($Startday+$value),$Startyear);
        $RangeDates = date("mdY", $Rangetimestamps);
                $RangeDayNumber = date("d", $Rangetimestamps);
                $RangeDayText=  date("D", $Rangetimestamps);
                $RangeMonths = date("m", $Rangetimestamps);




If ($RangeDayText == "Thu") {
      $first = 0;
      $last = 0;
      if ($every2nd == TRUE) {
             $first = 8;
             $last = 14;
      }
      if ($every3rd == TRUE) {
             $first = 15;
             $last = 21;
      }
      if ($every4th == TRUE) {
             $first = 22;
             $last = 28;
      }
      if ($everyLast == TRUE) {
             $last = date("t", $Rangetimestamps);
             $first = $last - 6; // Yes 6 because we include first and last days in the check
      }
      if ($first > 0) {
            if (($RangeDayNumber >= ($first + date("d", $Rangetimestamps)) and
                ($RangeDayNumber <= ($last + date("d", $Rangetimestamps))) {
                    echo $RangeDayText." Text";
                   echo $RangeDates."  ".$RangeDayNumber."<p>";
            }
      } else {
      echo $RangeDayText." Text";
      echo $RangeDates."  ".$RangeDayNumber."<p>";
}}
?>
0
 
LVL 7

Accepted Solution

by:
petoskey-001 earned 500 total points
ID: 9879087
Ahh sorry.  I didn't think you would take it right from there, I just typed that from my head into Expert Exchange.

Here is a version that parses...

-------------------------------
<?php

If ($RangeDayText == "Thu") {
      $first = 0;
      $last = 0;
      if ($every2nd == TRUE) {
             $first = 8;
             $last = 14;
      }
      if ($every3rd == TRUE) {
             $first = 15;
             $last = 21;
      }
      if ($every4th == TRUE) {
             $first = 22;
             $last = 28;
      }
      if ($everyLast == TRUE) {
             $last = date("t", $Rangetimestamps);
             $first = $last - 6; // Yes 6 because we include first and last days in the check
      }
      if ($first > 0) {
            if (($RangeDayNumber >= ($first + date("d", $Rangetimestamps))) and
                ($RangeDayNumber <= ($last +  date("d", $Rangetimestamps)))) {
                    echo $RangeDayText." Text";
                   echo $RangeDates."  ".$RangeDayNumber."<p>";
            }
      } else {
            echo $RangeDayText." Text";
            echo $RangeDates."  ".$RangeDayNumber."<p>";
      }
}
?>
0
 

Author Comment

by:cubbyboy57
ID: 9904342
Sorry I had a hard time figuring out what was wrong - it looked simple, but you were adding first and last to my increasing timestamp so they never fell inside the range. By making it

      if ($first > 0) {
            if (($RangeDayNumber >=  $first) and
                ($RangeDayNumber <=  $last)) {
                    echo $RangeDayText." Text";
                   echo $RangeDates."  ".$RangeDayNumber."<p>";
            }
      } else {
            echo $RangeDayText." Text";
            echo $RangeDates."  ".$RangeDayNumber."<p>";
      }

It seems to get me where I want...


Thanks for the help!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

757 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

19 Experts available now in Live!

Get 1:1 Help Now