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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need some good PHP Calendar code 5

Status
Not open for further replies.

cutley

Programmer
Jan 8, 2003
17
US
I am doing dynamic calendar using a MySQL database, and what I really need is some basic example code for display day, week, and month views in a traditional calendar format.

If the code already had places to plug in my dynamic data, that would be super cool.

I have been all over the Net, and nothing seems to fit this description. Anybody have experience with some free code that would meet this criteria or close?
 
Have you tried looking at some of the web mail programs. A lot of the ones I have looked at have calendars as well.

HTH Binky ::)
><>
 
I am working on a similar idea at that displays the month view.

If this is the kind of thing you are looking for, I could share the script in a day or two when I can get back to it and sanitize/secure it for the general public. I'm stuck on another project now.
 
jimoblak, that is exactly the kind of thing I'm looking for. I'd love to take a look at the code when you have the time to clean it up, thank you!

Binky, I hadn't thought of looking at Web Mail programs, I'll check that out. Thanks.
 
jimoblak - do you think you could share your code for the calendar? I'm writing a simple scheduling application for my office and have been fighting with what I already have. Would appreciate any insight!

Thanks, Paul
 
okay - 8 months is long enough to procrastinate...

Here is my clumsy code to create a calendar template. Use it as a reference of what to do and what not to do. It could/should be cleaned up to use arrays instead of repetitive print statements.

To simplify things, I took out the database function that displays events. It should be easy to put back in. Just query your database in the middle of the for loop on each date - - then print anything that gets returned.

Code:
<html><head><title>Calendar</title></head><body>

<?php

$today=date(Ymd);
$calMonth=$_GET[calMonth];
$calYear=$_GET[calYear];

if ((!$calMonth) && (!$calYear)) {
$calMonth = date (&quot;m&quot;);
$calYear = date (&quot;Y&quot;);
}

$monthlydate=$calYear.$calMonth.&quot;00&quot;;
$ThisMonth=$calYear . $calMonth;
$ThisMonth=($ThisMonth*100);

//  Calculate the viewed Month.
$Timestamp = mktime (0, 0, 0, $calMonth, 1, $calYear);
$MonthName  =  date(&quot;F&quot;, $Timestamp);

//  Make a table with the proper month.
print (&quot;<P ALIGN=LEFT><TABLE BORDER=0 CELLPADDING=1 WIDTH=97% CELLSPACING=0 BGCOLOR=BLACK ALIGN=CENTER><tr><td><TABLE BORDER=0 CELLPADDING=2 WIDTH=100% CELLSPACING=1 ALIGN=CENTER><TR BGCOLOR=BLACK><TD ALIGN=CENTER COLSPAN=7><FONT SIZE=4 COLOR=WHITE><B>$MonthName $calYear</B></FONT></TD></TR><TR BGCOLOR=BLACK><TD ALIGN=CENTER WIDTH=15%><B><FONT SIZE=2 COLOR=WHITE>Sun</FONT></B></TD><TD ALIGN=CENTER WIDTH=14%><B><FONT SIZE=2 COLOR=WHITE>Mon</FONT></B></TD><TD ALIGN=CENTER WIDTH=14%><B><FONT SIZE=2 COLOR=WHITE>Tue</FONT></B></TD><TD ALIGN=CENTER WIDTH=14%><B><FONT SIZE=2 COLOR=WHITE>Wed</FONT></B></TD><TD ALIGN=CENTER WIDTH=14%><B><FONT SIZE=2 COLOR=WHITE>Thu</FONT></B></TD><TD ALIGN=CENTER WIDTH=14%><B><FONT SIZE=2 COLOR=WHITE>Fri</FONT></B></TD><TD ALIGN=CENTER WIDTH=15%><B><FONT SIZE=2 COLOR=WHITE>Sat</FONT></B></TD></TR>\n&quot;);

$MonthStart = date(&quot;w&quot;, $Timestamp);
if ($MonthStart == 0) {
$MonthStart = 7;
}
$LastDay = date(&quot;d&quot;, mktime (0, 0, 0, $Month+1, 0, $Year));
$calStartDate = -$MonthStart;
for ($k = 1; $k <= 6; $k++) {

//SET 6 ROWS
print (&quot;<TR BGCOLOR=WHITE>&quot;);
for ($i = 1; $i <= 7; $i++) {

//SET 7 COLUMNS
$calStartDate++;
if (($calStartDate <= 0) || ($calStartDate > $LastDay)) {
print (&quot;<TD BGCOLOR=#DDDDDD height=110 valign=top align=left>&nbsp</TD>&quot;);
} elseif (($calStartDate >= 1) && ($calStartDate <= $LastDay)) {

//IF TODAY
if ($calStartDate==&quot;1&quot;) {$MyDay=&quot;01&quot;;}
if ($calStartDate==&quot;2&quot;) {$MyDay=&quot;02&quot;;}
if ($calStartDate==&quot;3&quot;) {$MyDay=&quot;03&quot;;}
if ($calStartDate==&quot;4&quot;) {$MyDay=&quot;04&quot;;}
if ($calStartDate==&quot;5&quot;) {$MyDay=&quot;05&quot;;}
if ($calStartDate==&quot;6&quot;) {$MyDay=&quot;06&quot;;}
if ($calStartDate==&quot;7&quot;) {$MyDay=&quot;07&quot;;}
if ($calStartDate==&quot;8&quot;) {$MyDay=&quot;08&quot;;}
if ($calStartDate==&quot;9&quot;) {$MyDay=&quot;09&quot;;}
if ($calStartDate>9) {$MyDay=$calStartDate;}
$ThisDay=&quot;$calYear$calMonth$MyDay&quot;;
if (($MyDay==date(&quot;d&quot;)) AND ($calMonth==date(&quot;m&quot;)) AND ($calYear==date(&quot;Y&quot;))) {
print (&quot;<TD BGCOLOR=#FFCCCC height=110 valign=top align=left><FONT SIZE=2><b>$calStartDate</b></font>\n&quot;);
} else {
print (&quot;<TD height=110 valign=top align=left><FONT SIZE=2><b>$calStartDate</b></font>\n&quot;);
}

print (&quot;</TD>&quot;);
}
}
print (&quot;</TR>\n&quot;);
}
print (&quot;</TABLE></td></tr><TR BGCOLOR=WHITE><TD>\n&quot;);

// Make the form.
print (&quot;<FORM METHOD=GET>\n&quot;);
print (&quot;Select another month to view:\n&quot;);
print (&quot;<SELECT NAME=calMonth>\n&quot;);
if ($calMonth==&quot;12&quot;) { print (&quot;<OPTION SELECTED VALUE=01>January</OPTION>\n&quot;); } else { print (&quot;<OPTION VALUE=01>January</OPTION>\n&quot;); }
if ($calMonth==&quot;01&quot;) { print (&quot;<OPTION SELECTED VALUE=02>February</OPTION>\n&quot;);} else { print (&quot;<OPTION VALUE=02>February</OPTION>\n&quot;); }
if ($calMonth==&quot;02&quot;) { print (&quot;<OPTION SELECTED VALUE=03>March</OPTION>\n&quot;); } else { print (&quot;<OPTION VALUE=03>March</OPTION>\n&quot;); }
if ($calMonth==&quot;03&quot;) { print (&quot;<OPTION SELECTED VALUE=04>April</OPTION>\n&quot;); } else { print (&quot;<OPTION VALUE=04>April</OPTION>\n&quot;); }
if ($calMonth==&quot;04&quot;) { print (&quot;<OPTION SELECTED VALUE=05>May</OPTION>\n&quot;); } else { print (&quot;<OPTION VALUE=05>May</OPTION>\n&quot;); }
if ($calMonth==&quot;05&quot;) { print (&quot;<OPTION SELECTED VALUE=06>June</OPTION>\n&quot;); } else { print (&quot;<OPTION VALUE=06>June</OPTION>\n&quot;); }
if ($calMonth==&quot;06&quot;) { print (&quot;<OPTION SELECTED VALUE=07>July</OPTION>\n&quot;); } else { print (&quot;<OPTION VALUE=07>July</OPTION>\n&quot;); }
if ($calMonth==&quot;07&quot;) { print (&quot;<OPTION SELECTED VALUE=08>August</OPTION>\n&quot;); } else { print (&quot;<OPTION VALUE=08>August</OPTION>\n&quot;); }
if ($calMonth==&quot;08&quot;) { print (&quot;<OPTION SELECTED VALUE=09>September</OPTION>\n&quot;); } else { print (&quot;<OPTION VALUE=09>September</OPTION>\n&quot;); }
if ($calMonth==&quot;09&quot;) { print (&quot;<OPTION SELECTED VALUE=10>October</OPTION>\n&quot;); } else { print (&quot;<OPTION VALUE=10>October</OPTION>\n&quot;); }
if ($calMonth==&quot;10&quot;) { print (&quot;<OPTION SELECTED VALUE=11>November</OPTION>\n&quot;); } else { print (&quot;<OPTION VALUE=11>November</OPTION>\n&quot;); }
if ($calMonth==&quot;11&quot;) { print (&quot;<OPTION SELECTED VALUE=12>December</OPTION>\n&quot;); } else { print (&quot;<OPTION VALUE=12>December</OPTION>\n&quot;); }
print (&quot;</SELECT>\n&quot;);
print (&quot;<SELECT NAME=calYear>\n&quot;);

$MyNextYear=$calYear+1;
$ThisYear=date(&quot;Y&quot;);
if ($calMonth==&quot;12&quot;) {
print (&quot;<OPTION SELECTED VALUE=$MyNextYear>$MyNextYear</OPTION>\n&quot;);
} else {
print (&quot;<OPTION SELECTED VALUE=$calYear>$calYear</OPTION>\n&quot;);
}

for ($nextyear = $ThisYear; $nextyear <= 2015; $nextyear++) {
print (&quot;<OPTION VALUE=$nextyear>$nextyear</OPTION>\n&quot;);
}

print (&quot;</SELECT><INPUT TYPE=SUBMIT NAME=SUBMIT VALUE=\&quot;Display\&quot;></FORM></TD></TR></TABLE></P>\n&quot;);
?>

</body></html>

- - picklefish - -

Why is everyone in this forum responding to me as picklefish?
 
You stated in the middle of the for loop right?

--- code snippet ---
for ($k = 1; $k <= 6; $k++) {

//SET 6 ROWS
print (&quot;<TR BGCOLOR=WHITE>&quot;);
for ($i = 1; $i <= 7; $i++) {

//SET 7 COLUMNS
$calStartDate++;
if (($calStartDate <= 0) || ($calStartDate > $LastDay)) {
print (&quot;<TD BGCOLOR=#DDDDDD height=110 valign=top align=left> </TD>&quot;);
} elseif (($calStartDate >= 1) && ($calStartDate <= $LastDay)) {

//IF TODAY
if ($calStartDate==&quot;1&quot;) {$MyDay=&quot;01&quot;;}
if ($calStartDate==&quot;2&quot;) {$MyDay=&quot;02&quot;;}
if ($calStartDate==&quot;3&quot;) {$MyDay=&quot;03&quot;;}
if ($calStartDate==&quot;4&quot;) {$MyDay=&quot;04&quot;;}
if ($calStartDate==&quot;5&quot;) {$MyDay=&quot;05&quot;;}
if ($calStartDate==&quot;6&quot;) {$MyDay=&quot;06&quot;;}
if ($calStartDate==&quot;7&quot;) {$MyDay=&quot;07&quot;;}
if ($calStartDate==&quot;8&quot;) {$MyDay=&quot;08&quot;;}
if ($calStartDate==&quot;9&quot;) {$MyDay=&quot;09&quot;;}
if ($calStartDate>9) {$MyDay=$calStartDate;}
$ThisDay=&quot;$calYear$calMonth$MyDay&quot;;
if (($MyDay==date(&quot;d&quot;)) AND ($calMonth==date(&quot;m&quot;)) AND ($calYear==date(&quot;Y&quot;))) {
print (&quot;<TD BGCOLOR=#FFCCCC height=110 valign=top align=left><FONT SIZE=2><b>$calStartDate</b></font>\n&quot;);
} else {
print (&quot;<TD height=110 valign=top align=left><FONT SIZE=2><b>$calStartDate</b></font>\n&quot;);
}

print (&quot;</TD>&quot;);
}
}
print (&quot;</TR>\n&quot;);
}
print (&quot;</TABLE></td></tr><TR BGCOLOR=WHITE><TD>\n&quot;);

--- end snippet ---

I tried putting it in the middle of that and when it parsed the file, I had several 10/07's, instead of just 1 10/07 with multiple entries.

I used this:

while ($row = mysql_fetch_row($run_sql))
{

$empname = $row[1];
$shiftstart = $row[3];
$shiftend = $row[4];

print (&quot;<TD BGCOLOR=#FFCCCC height=110 valign=top align=left><FONT SIZE=2><b>$calStartDate</b><p>$empname :: $shiftstart - $shiftend</p></font>\n&quot;);

}

Am I missing something here?
 
Sorry - 'the middle' was too vague. You would run your query within the table cell for the date. Your code is creating several cells within your while. Flip-flop this thinking & mimic the following...

Code:
print (&quot;<TD BGCOLOR=#FFCCCC height=110 valign=top align=left><FONT SIZE=2><b>$calStartDate</b></font>\n&quot;);
} else {
print (&quot;<TD height=110 valign=top align=left><FONT SIZE=2><b>$calStartDate</b></font>\n&quot;);
}

//CALL DATABASE HERE AND RETURN EVENT TITLE(S)

print (&quot;</TD>&quot;);


- - picklefish - -

Why is everyone in this forum responding to me as picklefish?
 
Hey just to let you know, you probably already know this, but on this line:

$LastDay = date(&quot;d&quot;, mktime (0, 0, 0, $Month+1, 0, $Year));

It will always display 31 days regardless of the month... I changed it to this:

$LastDay = date(&quot;d&quot;, mktime (0, 0, 0, $calMonth+1, 0, $Year));

Which printed the correct # of days for each month including leap years. Just thought I'd help out!
 
Thanks for noticing that variable.

- - picklefish - -

Why is everyone in this forum responding to me as picklefish?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top