Help with PHP calendar

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Messian
Forum Newbie
Posts: 3
Joined: Wed Aug 20, 2003 1:47 pm

Help with PHP calendar

Post by Messian »

Hello, I'm writing an app that allows the user to select a day from a calendar. It should only display one week out from the current day. It works great, however, when it reaches within 7-8 days of the end of the month, it blows up(doesn't print any days at all). Any help would be great.

Code: Select all

<?php
<?php
session_start(); 
header("Cache-control: private"); //IE 6 Fix 

// If the $Month and $Year values don't exist, make them the current month and year.

 	$Month = date ("m");
 	$Year = date ("Y");
	$Day = date("d");

//  Calculate the viewed Month.
$Timestamp = mktime (0, 0, 0, $Month, $Day, $Year);
$MonthName  =  date("F", $Timestamp);
//  Make a table with the proper month.
?>
<form action="Sched2.php" method="post">
<CENTER><TABLE>
<TR>
<TD>
<?


print ("<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 ALIGN=CENTER>");
print ("<TR BGCOLOR=BLUE><TD COLSPAN=7 ALIGN=CENTER><FONT COLOR=WHITE><B>$MonthName $Year</B></FONT></TD></TR>");
print ("<TR BGCOLOR=BLUE><TD ALIGN=CENTER WIDTH=20><B><FONT COLOR=WHITE>Su</FONT></B></TD><TD ALIGN=CENTER WIDTH=20><B><FONT COLOR=WHITE>M</FONT></B></TD><TD ALIGN=CENTER WIDTH=20><B><FONT COLOR=WHITE>Tu</FONT></B></TD><TD ALIGN=CENTER WIDTH=20><B><FONT COLOR=WHITE>W</FONT></B></TD><TD ALIGN=CENTER WIDTH=20><B><FONT COLOR=WHITE>Th</FONT></B></TD><TD ALIGN=CENTER WIDTH=20><B><FONT COLOR=WHITE>F</FONT></B></TD><TD ALIGN=CENTER WIDTH=20><B><FONT COLOR=WHITE>Sa</FONT></B></TD></TR>\n");
$MonthStart = date("w", $Timestamp);
if ($MonthStart == 0) {
 	$MonthStart = 7;
}
$LastDay = date("d", mktime (0, 0, 0, $Month, $Day+8, $Year));
$StartDate = -$MonthStart;

$bgRGB = "white";
for ($k = 1; $k <= 2; $k++) 
{ // Print 2 rows.
 	print ("<TR BGCOLOR=WHITE>");
 	for ($i = 1; $i <= 7; $i++) 
	{ // Use 7 columns
 		$StartDate++;
 		if (($StartDate <= 0) || ($StartDate > $LastDay)) 
		{
 			print ("<TD BGCOLOR=GREEN>&nbsp</TD>");
 		} 
		elseif ( ($StartDate >= 1) &&($Day < $LastDay  ))
			{
 			print ("<TD ALIGN=CENTER BGCOLOR="$bgRGB"><input type="Submit" name="SchedDay" value="$Day"></TD>");
            $Day++;
											
            if ($Day >= date("t",$Timestamp))
            {
             	$Day = 1;
                $bgRGB = "yellow";
            }
 		}
 	}
 	print ("</TR>\n");
}
print ("</TABLE>\n");
?>
</TD>
</table></CENTER></form>

Yellow background on calendar is the start of a new month.



?>
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

it looks like it's trying to get one week ONLY within the current month. have ti check how far it is from the end of the month. if it's less than 8 days, take this month and the next from the db, and first will in from this month, then finish with next
Post Reply