Jump to content

highlight dates on calendar.. help! please...


Dragen

Recommended Posts

Hi,

I've got this code for my calendar. It basically displays a whole year in one go instead of just a month.

What I am wanting to do is to query some dates whilst the script is writing the calendar. Such as if the date is a certain date, then it adds a snippet of code to the table cell.

I'm not sure that I can do this with how the calendar is coded...

My attempt at it is the "f($day == $booked){" section, although I couldn't figure out what to set $booked as to highlight the correct date or if this was the right way to do this.

If I set $booked to a number, lets say 3, then it will ad the code to the 3rd day in every month. The problem is I want to specify exactly what days to highlight. Like the 3rd of march 2007..

This is the code I've got:

<?php
//this function displays the calendar
function showYear($month, $year){
?>
    <table border="1" cellpadding="0" cellspacing="0" width="100%" id="calendar">
    	<tr>
		<th width="16%" height="48" align="center" valign="bottom"><?php echo $year; ?></th>
		<th align="center" valign="middle">Sunday</th>
		<th align="center" valign="middle">Monday</th>
		<th align="center" valign="middle">Tuesday</th>
		<th align="center" valign="middle">Wednesday</th>
		<th align="center" valign="middle">Thursday</th>
		<th align="center" valign="middle">Friday</th>
		<th align="center" valign="middle">Saturday</th>
	</tr>
<?php
//check if $month is above 0 but below 13. If so it writes out a month, and increments $month by one.
//once it reaches 12 it stops.
while($month > 0 && $month < 13)
{
showMonth($month, $year);
$month++;
}
?>
</table>
<?php
}

//This function writes the months
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 "\t\t<tr>\n";
	//displays month
	echo "\t\t\t<td align=\"right\"><strong>". date(F, $date) . "</strong></td>\n";

    for($i = 1; $i <= $offset; $i++)
    {
	echo "\t\t\t<td width=\"12%\"></td>\n";
    }
    for($day = 1; $day <= $daysInMonth; $day++)
    {
        if( ($day + $offset - 1) % 7 == 0 && $day != 1)
        {
	echo "\t\t</tr>\n";
	echo "\t\t<tr>\n";
	echo "\t\t\t<td></td>\n";
            $rows++;
        }
	//checks if date is booked. If so sets it to class="booked"
	echo "\t\t\t<td align=\"center\" valign=\"middle\" width=\"12%\" height=\"48\"";
	if($day == $booked){
	echo " class=\"booked\"";
	}
	echo ">" . $day . "</td>\n";
    }
    while( ($day + $offset) <= $rows * 7)
    {
	echo "\t\t\t<td></td>\n";
        $day++;
    }
	echo "\t\t</tr>\n";
}
?>

If anyone could help me figure this out,

thanks!

Link to comment
https://forums.phpfreaks.com/topic/46057-highlight-dates-on-calendar-help-please/
Share on other sites

ah thanks! that's working much better.. I'm pretty useless with arrays at the moment.

so what I've got is

$booked[$month] = array($booked_day1 , $booked_day2 , $booked_day3 );

and

if (in_array($day , $booked[$month])){

although if I set $booked_day1 to 1 then it still highlights all of the first days for every month. $month is already a variable I'm using so should I use perhaps $booked_month, or should it be the same?

I need to be able to use this eventually to read the dates to highlight from a database.

 

Thanks

Just so you know, the database table I'm wanting to read from looks like this:

bookID | day | month | year
---------------------------
  1    |  2  |   1   | 2007
  2    | 12  |   11  | 2007
  4    |  4  |   6   | 2008

with bookID as a primary key. I'm trying to get my calendar to read the input from the day month and year columns and highlight those days on the calendar.

 

I thought that I should somehow be able to check the day, month and year against the date that is being written, but still can't figure out how.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.