...

View Full Version : Coloring today and week-end in a calendar...



Peuplarchie
12-02-2008, 07:45 AM
Good day to you all,
I'm working on a calendar script.
I'm facing 2 questions;

1. How, if is this month, can I have the day in a different color ?
2. how could I have the Saturday and Sunday show in different color ?


Here is my code:



<?
function showMonth($month, $year)
{
$date = mktime(12, 0, 0, $month, 1, $year);
$daysInMonth = date("t", $date);
// calculate the position of the first day in the calendar (sunday = 1st column, etc)
$offset = date("w", $date);
$rows = 1;

echo "<table border=\"1\" align=\"center\">\n";
echo "<tr><td colspan=\"7\"><h1>" . date("F Y", $date) . "</h1></td></tr>";
echo "<tr><th>Sunday</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th></tr>";
echo "\n\t<tr>";
for($i = 1; $i <= $offset; $i++)
{
echo "<td></td>";
}
for($day = 1; $day <= $daysInMonth; $day++)
{
if( ($day + $offset - 1) % 7 == 0 && $day != 1)
{
echo "</tr>\n\t<tr>";
$rows++;
}

echo "<td valign=\"top\"><table border=\"0\" align=\"center\">";
echo "<tr><td width=\"100\" valign=\"top\" bgcolor=\"cccc99\">" . $day . "</td></tr>";
echo "<tr><td width=\"100\" height=\"100\" valign=\"top\"><br/></td></tr>";
echo "</table></td>";

}
while( ($day + $offset) <= $rows * 7)
{
echo "<td></td>";
$day++;
}
echo "</tr>\n";
echo "</table>\n";
}
showmonth(12, 2008);
?>




Thanks !

abduraooft
12-02-2008, 07:59 AM
1. How, if is this month, can I have the day in a different color ?

date("n"); would give the numeric representation of current month. So you could compare this value with the received value of $month and apply a style accordingly.

2. how could I have the Saturday and Sunday show in different color ?
Similar to the above, date("w"); would give the values from 0-6 representing Sunday-Saturday. So you could compare this value with your loop counter $day

Refer the manual for date() (http://php.net/date)

Peuplarchie
12-02-2008, 08:22 AM
Here is where I'm at whit your help...



<?
function showMonth($month, $year)
{
$date = mktime(12, 0, 0, $month, 1, $year);
$daysInMonth = date("t", $date);
// calculate the position of the first day in the calendar (sunday = 1st column, etc)
$offset = date("w", $date);
$rows = 1;

echo "<table border=\"1\" align=\"center\">\n";
echo "<tr><td colspan=\"7\"><h1>" . date("F Y", $date) . "</h1></td></tr>";
echo "<tr><th>Sunday</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th></tr>";
echo "\n\t<tr>";
for($i = 1; $i <= $offset; $i++)
{
echo "<td></td>";
}
for($day = 1; $day <= $daysInMonth; $day++)
{
if( ($day + $offset - 1) % 7 == 0 && $day != 1)
{
echo "</tr>\n\t<tr>";
$rows++;
}
if ($month == date("m")){
if ($day == 0 || $day == 6){
echo "<td valign=\"top\"><table border=\"0\" align=\"center\">";
echo "<tr><td width=\"100\" valign=\"top\" bgcolor=\"999966\">" . $day . "</td></tr>";
echo "<tr><td width=\"100\" height=\"100\" valign=\"top\"><br/></td></tr>";
echo "</table></td>";
}
}else{
echo "<td valign=\"top\"><table border=\"0\" align=\"center\">";
echo "<tr><td width=\"100\" valign=\"top\" bgcolor=\"cccc99\">" . $day . "</td></tr>";
echo "<tr><td width=\"100\" height=\"100\" valign=\"top\"><br/></td></tr>";
echo "</table></td>";

}
}
while( ($day + $offset) <= $rows * 7)
{
echo "<td></td>";
$day++;
}
echo "</tr>\n";
echo "</table>\n";
}
showmonth(12, 2008);
?>

Peuplarchie
12-02-2008, 08:33 AM
I foud it thanks you !



if ($month == date("m") && $day == date("d")){
echo "<td valign=\"top\"><table border=\"0\" align=\"center\">";
echo "<tr><td width=\"100\" valign=\"top\" bgcolor=\"999966\">" . $day . "</td></tr>";
echo "<tr><td width=\"100\" height=\"100\" valign=\"top\"><br/></td></tr>";
echo "</table></td>";

}else{
echo "<td valign=\"top\"><table border=\"0\" align=\"center\">";
echo "<tr><td width=\"100\" valign=\"top\" bgcolor=\"cccc99\">" . $day . "</td></tr>";
echo "<tr><td width=\"100\" height=\"100\" valign=\"top\"><br/></td></tr>";
echo "</table></td>";

}



Am i doing it right ?

mlseim
12-02-2008, 01:16 PM
This is the same thing, (but yours would work too).



$bg="#cccc99";

// Calculate Saturday or Sunday here, and change $bg accordingly.

if ($month == date("m") && $day == date("d")){
$bg="#999966";
}
echo "
<td valign='top'><table border='0' align='center'>
<tr><td width='100' valign='top' bgcolor='$bg'> $day </td></tr>
<tr><td width='100' height='100' valign='top'><br/></td></tr>
</table></td>
";
}

abduraooft
12-02-2008, 01:40 PM
It'd be more effective, if you execute date() functions and the comparison outside the loop.

mlseim
12-02-2008, 01:55 PM
I also wonder why they are trying to re-invent the millions of other PHP calendar scripts.
Maybe it's a good learning exercise?

Peuplarchie
12-16-2008, 05:59 AM
learning and to have your own...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum