Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 379
  • Last Modified:

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

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
James Stone
Asked:
James Stone
  • 2
  • 2
1 Solution
 
petoskey-001Commented:
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
 
James Stonewarehouse managerAuthor Commented:
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
 
petoskey-001Commented:
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
 
James Stonewarehouse managerAuthor Commented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now