Solved

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

Posted on 2003-12-04
4
375 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
[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
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

617 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