Jump to content

PHP Calendar Highlighting Dates


dprichard

Recommended Posts

I am working on a calendar and want to show times that employees have requested off by changing the color and highlighting the days off.  So, if the employee has off from Jan 1st - 5th, then those days will be highlighted.  I have this working, but the problem I am running into is that if they requested off for Jan 1st - 5th and Jan 20th - 25th, then only one request shows highlighted.  The other request does not.  Any help from you PHP Gurus would be greatly appreciated.

 

<?php
include '../config.php';
include ('../functions.php');
permission_admin();

// Pulls in employee information from the database.
$employees = mysql_query("SELECT emp_id, emp_fname, emp_lname FROM employee ORDER BY emp_lname ASC") or die(mysql_error());
$row_employees = mysql_fetch_array($employees);

//Variables used in testing to show every month in 2008
$firstmonth = 1;
$lastmonth = 12;

while ($firstmonth <= $lastmonth) { 

//Starts the first calendar at Jan 2008 for Testing
$date = strtotime("1-$firstmonth-2008");

//$date = time();

//This puts the day, month, and year in seperate variables
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);

//Generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year);

//Generate the month
$title = date('F', $first_day);

//Find out what day of the week the first day falls on
$day_of_week = date('D', $first_day);

//Get the days in the current month
$days_in_month = cal_days_in_month(0, $month, $year);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>Calendar</title>
<style>
	.calendar_table {
		border: solid;
		border-color: #0c4a93;
		border-size: 1px;
		margin: 0 0 30px 0;
		}
	.calendar_table td {
	}
	.cal_month_title {
			background-color: #0c4a93;
			text-align: center;
			font-size: 12px;
			color: #FFFFFF;
			font-family: Verdana;
	}
	.cal_day_title {
		background-color: #688ebd;
		text-align: center;
		font-size: 12px;
		color: #FFFFFF;
		font-family: Verdana;
		font-weight: bold;
		padding: 0 8px 0 8px;
		width: 3%;
	}
	.cal_day_blank {
		background-color: #999999;
		width: 3%;
	}
	.cal_day_current {
		background-color: #d3481f;
		text-align: center;
		font-size: 12px;
		color: #FFF;
		font-family: Verdana;
		font-weight: bold;
	}
	.cal_day {
		background-color: #bccde1;
		text-align: center;
		font-size: 12px;
		color: #FFF;
		font-family: Verdana;
		font-weight: bold;
	}
	.cal_emp_title {
		background-color: #688ebd;
		text-align: right;
		font-size: 12px;
		color: #FFFFFF;
		font-family: Verdana;
		font-weight: bold;
		padding: 0 8px 0 8px;
	}
	.cal_emp_name {
		background-color: #8fb0d9;
		text-align: right;
		font-size: 12px;
		color: #FFFFFF;
		font-family: Verdana;
		font-weight: bold;
		padding: 0 8px 0 8px;
	}
</style>
</head>
<body>
<?php 
echo "<table width='100%' class='calendar_table'>";
echo "<tr><th colspan=42 class='cal_month_title'> $title $year </th></tr>";
echo "<tr>";
	$day_count = 1;

		echo "<tr><td nowrap class='cal_emp_title'>Employee Name</td>";

	//Sets the first day of the month to 1
	$day_num = 1;

	//count up the days, until we have done them all in the month
	while ($day_num <= $days_in_month) {
		echo "<td class='cal_day_title'>".date('D', mktime(0,0,0,$month, $day_num, $year))."</td>";
		$day_num++;
		$day_count++;

	} echo "</tr>";

$day_count = 1;

		do {
		echo "<tr><td nowrap class='cal_emp_name'>".$row_employees['emp_id']." - ".$row_employees['emp_lname'].", ".$row_employees['emp_fname']."</td>";
		$emp_id = '';
		$emp_id = mysql_real_escape_string($row_employees['emp_id']);
		$time_off = mysql_query("SELECT to_request_start_date, to_request_end_date, to_request_id FROM to_request WHERE to_request_emp_id = '$emp_id'") or 		die(mysql_error());
		$row_time_off = mysql_fetch_array($time_off);
		$request_start = $row_time_off['to_request_start_date'];
		$request_end = $row_time_off['to_request_end_date'];

//Sets the first day of the month to 1
$day_num = 1;

//count up the days, until we have done them all in the month
while ($day_num <= $days_in_month) {
                $comp_date = strtotime($year . '-' . $month . '-' . $day_num);
                $request_start_comp = strtotime($request_start);
                $request_end_comp = strtotime($request_end);
                $start = date('Y-m-j',strtotime($request_start));
                $end = date('Y-m-j',strtotime($request_end));
	if ($comp_date >= $request_start_comp && $comp_date <= $request_end_comp) { 
		echo "<td class='cal_day_current'>"; 
		echo $day_num; 
		echo"</td>";
		} else { 
			echo "<td class='cal_day'>"; 
			echo $day_num; }
	$day_num++;
	$day_count++;
   }


while ( $day_count >1 && $day_count <=7 )
{
echo "<td class='cal_day_blank'>   </td>";
$day_count++;
}

echo "</tr>";

} while ($row_employees = mysql_fetch_array($employees));

mysql_data_seek($employees, 0);


echo "</table>";

$firstmonth++;
}
?>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/89902-php-calendar-highlighting-dates/
Share on other sites

I was considering storing it by day but really couldn't wrap my mind around how to make it easy for people to input request.  I didn't want them to have to put in that they wanted off Monday the 1st, tuesday the 2nd, and wednesday the 3rd.  I just wanted them to be able to put in a start and end date for their request to simplify things.  Thoughts?

Kind of a newb, so I really appreciate your patience and input. 

 

Would I split it up into days with PHP or MySQL?

 

When I store it, I need to tie it all together with 1 request number so I am assuming I would need to have one record for each day correct?

 

If so, each record would have a different number generated for the auto number primary key.  How would I tie these together into one request?

 

 

That's how I do my reservation calendar, in a sense they are the same in my mind.  Someone has a vacation day, it stores that day in the DB along with their user id.  When you populate your calendar you look for entries for that day and show it.

I guess that depends on what you are using your primary key for.

 

If you load an array for the month, set the array index to the day of the month:

 

array[1] would contain the first day of the month's entries.  I would also assume multiple entries could be for the same day, so you would need a multi demensional array

 

array[1][Tom's day off]

array[1][Fred dr appt]

 

array[2][Joes day off]

 

etc

 

And when you loop through the days, do a check

 

if array[$x] == day of week, display data.

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.