Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Third Saturday

Status
Not open for further replies.

SBuzzT

Programmer
Aug 24, 2005
86
CA
I need to be able to show the date for the third Saturday of each month. Anyone have any idea how to do this fairly simply?
 
how about this

Code:
$year = "2007";
$month ="10";

$sat = strtotime("+2 weeks", strtotime("next Saturday", strtotime("yesterday", strtotime("$year-$month-01")));

have not tested it but this feels ok.

it basically says:

1. take the first day of the month to test,
2. move to the day before (last day of previous month)
3. move to the next saturday (first saturday of month)
4. move forward 2 weeks (third saturday).





 
jpadie - it's close, but fails if there are more than 4 saturdays in a month.
 
i have just tested this code and it works fine. it's the same as above
Code:
<?php
$year = "2007";
$month = "01";
echo '<table>';
for ($i=$month; $i <= 12; $i++){
$sat3 = strtotime("+2 weeks",strtotime ("next Saturday", strtotime("yesteday", strtotime("$year-$i-01"))));
echo '<tr><td>'.$i.'</td><td>'.date("D, d M Y", $sat3).'</td></tr>';
}
echo '</table>';
?>

it outputs

Code:
01	Sat, 20 Jan 2007
2	Sat, 17 Feb 2007
3	Sat, 17 Mar 2007
4	Sat, 21 Apr 2007
5	Sat, 19 May 2007
6	Sat, 16 Jun 2007
7	Sat, 21 Jul 2007
8	Sat, 18 Aug 2007
9	Sat, 15 Sep 2007
10	Sat, 20 Oct 2007
11	Sat, 17 Nov 2007
12	Sat, 15 Dec 2007
 
Maybe it's the version of php I'm using. I have easyphp installed on my local system running php 4.3. Here's what I get.
Code:
Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\sites\tests\date.php on line 7

Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\sites\tests\date.php on line 7

Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\sites\tests\date.php on line 7

Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\sites\tests\date.php on line 7

Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\sites\tests\date.php on line 7

Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\sites\tests\date.php on line 7

Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\sites\tests\date.php on line 7

Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\sites\tests\date.php on line 7

Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\sites\tests\date.php on line 7

Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\sites\tests\date.php on line 7

Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\sites\tests\date.php on line 7

Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\sites\tests\date.php on line 7
01	
2	
3	
4	
5	
6	
7	
8	
9	
10	
11	
12
 
did you copy and paste the second set of code exactly? keeping the input variables the same?

there is an issue with dates prior to the 1/1/1970. this date is the putative beginning of the unix epoch which is the type of date stamp used by strtotime and date(). if you are trying to find dates before 1970 then you will need to upgrade your installation or use a different method.

the code i posted works just fine on my machine (php 5.2) but i can't see why it would not work going back to php4 (when strtotime was introduced)
 
Copied exactly. Just tried again with same result.
 
OK... I found the main error. yesterday was spelled yesteday on line 6.

Here the result now:

01 Sat, 27 Jan 2007
2 Sat, 24 Feb 2007
3 Sat, 24 Mar 2007
4 Sat, 21 Apr 2007
5 Sat, 26 May 2007
6 Sat, 23 Jun 2007
7 Sat, 21 Jul 2007
8 Sat, 25 Aug 2007
9 Sat, 22 Sep 2007
10 Sat, 27 Oct 2007
11 Fri, 23 Nov 2007
12 Sat, 22 Dec 2007
 
aha... this is a known bug in older versions of php (pre 4.4). here is some revised code that works around the bug (and simplifies the code)

Code:
<?
$year = "2007";
$month = "01";
echo '<table>';
for ($i=$month; $i <= 12; $i++){
$sat3 = strtotime ("+ 3 Saturday", strtotime("yesterday", strtotime("$year-$i-01")));
echo '<tr><td>'.$i.'</td><td>'.date("D, d M Y", $sat3).'</td></tr>';
}
echo '</table>';
?>
 
So close, but... (I really appreciate the help). What is the bug causing the issue? Maybe I'll just upgrade the php...
Code:
01	Sat, 20 Jan 2007
2	Sat, 17 Feb 2007
3	Sat, 17 Mar 2007
4	   Sat, 14 Apr 2007
5	Sat, 19 May 2007
6	Sat, 16 Jun 2007
7	   Sat, 14 Jul 2007
8	Sat, 18 Aug 2007
9	Sat, 15 Sep 2007
10	Sat, 20 Oct 2007
11	   Fri, 16 Nov 2007
12	Sat, 15 Dec 2007
 
By modifying your code just a bit I got better results (just november is off).

Code:
<?
$year = "2007";
$month = "01";
echo '<table>';
for ($i=$month; $i <= 12; $i++){
$sat3 = strtotime ("+ 3 Saturday", strtotime("$year-$i-01"));
echo '<tr><td>'.$i.'</td><td>'.date("D, d M Y", $sat3).'</td></tr>';
}
echo '</table>';
?>

I removed the yesterday part to get this result.
Code:
01	Sat, 20 Jan 2007
2	Sat, 17 Feb 2007
3	Sat, 17 Mar 2007
4	Sat, 21 Apr 2007
5	Sat, 19 May 2007
6	Sat, 16 Jun 2007
7	Sat, 21 Jul 2007
8	Sat, 18 Aug 2007
9	Sat, 15 Sep 2007
10	Sat, 20 Oct 2007
11	Fri, 16 Nov 2007
12	Sat, 15 Dec 2007
 
you need to have the yesterday in there to make sure you move back to the previous day. otherwise if the first day of a month is a saturday the +3 saturdays will take you to the fourth.

i do not know why my code on your machine is producing different results. i do not have an implementation of php going back to 4.2 to test it myself. there are known bugs in the implementation of strtotime within releases prior to 4.4.

it is advisable to update your php. both the 4.x and the 5.x code branches are maintained as public releases so you should be able to stick with the 4.x branch if you want.

there may be some locale issues also throwing a spanner in the works. but i don't see these on my machines - again, worth upgrading.

you can give up on the neat strtotime() implementation and instead calculate the third saturday iteratively. you would do something like this

Code:
echo "<table>";
for ($i=1; $i<=12; $i++){
 $fd = date("w", strtotime("2007-$i-01"));//calculate the first day of the month
 $sat3 = 21 - $fd;
 echo '<tr><td>'.$i.'</td><td>'.$sat3.'</td></tr>';
}
echo '</table>';

in fact, as i type this it looks more elegant than the strtotime version!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top