...

View Full Version : Php help with calendar



rjkdonaldson
12-06-2008, 08:21 AM
Need help with this php script for calendar. I need the Saturday and Sunday background color to be different from the rest of the calendar. The calendar uses a stylesheet to help in display. Where should I put the class code within the php? Help much appreciated.



<?php
function showCalendar(){
// Get key day informations.
// We need the first and last day of the month and the actual day
$today = getdate();
$firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
$lastDay = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));


// Create a table with the necessary header informations
echo '<table>';
echo ' <tr><th colspan="7">'.$today['month']." - ".$today['year']."</th></tr>";
echo '<tr class="days">';
echo ' <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
echo ' <td>Fr</td><td>Sa</td><td>Su</td></tr>';


// Display the first calendar row with correct positioning
echo '<tr>';
for($i=1;$i<$firstDay['wday'];$i++){
echo '<td>&nbsp;</td>';
}
$actday = 0;
for($i=$firstDay['wday'];$i<=7;$i++){
$actday++;
if ($actday == $today['mday']) {
$class = ' class="actday"';
} else {
$class = '';
}
echo "<td$class>$actday</td>";
}
echo '</tr>';

//Get how many complete weeks are in the actual month
$fullWeeks = floor(($lastDay['mday']-$actday)/7);

for ($i=0;$i<$fullWeeks;$i++){
echo '<tr>';
for ($j=0;$j<7;$j++){
$actday++;
if ($actday == $today['mday']) {
$class = ' class="actday"';
} else {
$class = '';
}
echo "<td$class>$actday</td>";
}
echo '</tr>';
}

//Now display the rest of the month
if ($actday < $lastDay['mday']){
echo '<tr>';

for ($i=0; $i<7;$i++){
$actday++;
if ($actday == $today['mday']) {
$class = ' class="actday"';
} else {
$class = '';
}

if ($actday <= $lastDay['mday']){
echo "<td$class>$actday</td>";
}
else {
echo '<td>&nbsp;</td>';
}
}


echo '</tr>';
}

echo '</table>';
}

showCalendar();
?>

abduraooft
12-06-2008, 09:20 AM
http://www.codingforums.com/showthread.php?t=153415 may help.

rjkdonaldson
12-06-2008, 10:25 AM
Thanks for the tip question is where do I place the code that will change bgcolor for Saturday and Sunday within the php I have already?

PHP6
12-06-2008, 04:21 PM
Thanks for the tip question is where do I place the code that will change bgcolor for Saturday and Sunday within the php I have already?

I have added just these three lines:


if (($i==6)or($i==7)) $class .= ' style="background-color:#FF0000"';
if (($j==5)or($j==6)) $class .= ' style="background-color:#FF0000"';
if (($i==5)or($i==6)) $class .= ' style="background-color:#FF0000"';


<?php
function showCalendar(){
// Get key day informations.
// We need the first and last day of the month and the actual day
$today = getdate();
$firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
$lastDay = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));


// Create a table with the necessary header informations
echo '<table>';
echo ' <tr><th colspan="7">'.$today['month']." - ".$today['year']."</th></tr>";
echo '<tr class="days">';
echo ' <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
echo ' <td>Fr</td><td>Sa</td><td>Su</td></tr>';


// Display the first calendar row with correct positioning
echo '<tr>';
for($i=1;$i<$firstDay['wday'];$i++){
echo '<td>&nbsp;</td>';
}
$actday = 0;
for($i=$firstDay['wday'];$i<=7;$i++){
$actday++;
if ($actday == $today['mday']) {
$class = ' class="actday"';
} else {
$class = '';
}
if (($i==6)or($i==7)) $class .= ' style="background-color:#FF0000"';
echo "<td$class>$actday</td>";
}
echo '</tr>';

//Get how many complete weeks are in the actual month
$fullWeeks = floor(($lastDay['mday']-$actday)/7);

for ($i=0;$i<$fullWeeks;$i++){
echo '<tr>';
for ($j=0;$j<7;$j++){
$actday++;
if ($actday == $today['mday']) {
$class = ' class="actday"';
} else {
$class = '';
}
if (($j==5)or($j==6)) $class .= ' style="background-color:#FF0000"';
echo "<td$class>$actday</td>";
}
echo '</tr>';
}

//Now display the rest of the month
if ($actday < $lastDay['mday']){
echo '<tr >';

for ($i=0; $i<7;$i++){
$actday++;
if ($actday == $today['mday']) {
$class = ' class="actday"';
} else {
$class = '';
}
if (($i==5)or($i==6)) $class .= ' style="background-color:#FF0000"';
if ($actday <= $lastDay['mday']){
echo "<td$class>$actday</td>";
}
else {
echo '<td>&nbsp;</td>';
}
}


echo '</tr>';
}

echo '</table>';
}

showCalendar();
?>

rjkdonaldson
12-06-2008, 11:08 PM
K dude will give it a go...have not tried it yet. Thanks.

rjkdonaldson
12-06-2008, 11:38 PM
That worked thanks heaps! Now was wondering how I could add events (tooltip style) to the calendar. Even though adding events as a separate php file is an option adding events using tooltips is preferred.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum