Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-12-04
4
Medium Priority
?
377 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 2000 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

Independent Software Vendors: 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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

730 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