Solved

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

Posted on 2003-12-04
4
372 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:James Stone
  • 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:James Stone
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:James Stone
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

685 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