Jump to content

Problem with Conditional Statement with Dates


dprichard

Recommended Posts

I have a if statement that checks to see if these two dates are the same and if so changes the table class to highlight that cell. The problem I am running into is that if the date range is 2008-03-17 - 2008-03-22 it matches those dates on my calender, but it is also highlighting 2008-03-2.  Almost like is says it is a match for the 2008-03-22 and throws away the second 2 in the day.  I need to force it to match just the 2008-03-22 and not 2008-03-2.  I have some extra stuff in there I was using to echo my results so I could see exactly what was going on, but my code is below.  I am basically just trying to highlight the days that fall between the two dates.

 

Any help would be greatly appreciated.

 

PHP 5

 

	while ($day_num <= $days_in_month) {
	if ($year."-".$month."-".$day_num >= date('Y-m-j', strtotime($request_start)) && $year."-".$month."-".$day_num <= date('Y-m-j', strtotime($request_end))) { 
		echo "<td class='cal_day_current'>"; 
		echo $year."-".$month."-".$day_num."<br //>".date('Y-m-j', strtotime($request_start))."<br //>".date('Y-m-j', strtotime($request_end))."<br //>".$row_time_off['emp_name']."<br //>".$row_time_off['to_request_id']; 
		echo"</td>";
		} else { 
			echo "<td class='cal_day'>"; 
			echo $year."-".$month."-".$day_num."<br //>".date('Y-m-j', strtotime($request_start))."<br //>".date('Y-m-j', strtotime($request_end)); }
	$day_num++;
	$day_count++;

 

 

Link to comment
Share on other sites

When you compare dates like that, you are comparing ASCII strings, you should be comparing UNIX time stamps:

<?php
while ($day_num <= $days_in_month) {
                $comp_date = strtotime($year . '-' . $month . '-' . $day);
                $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 $year."-".$month."-".$day_num."<br />". $start ."<br />". $end ."<br />".$row_time_off['emp_name']."<br />".$row_time_off['to_request_id']; 
		echo"</td>";
		} else { 
			echo "<td class='cal_day'>"; 
			echo $year."-".$month."-".$day_num."<br />".$start ."<br />". $end; }
	$day_num++;
	$day_count++;
        }
?>

 

Ken

Link to comment
Share on other sites

Thank you kenrbnsn, that worked.  I am running into one more weird issue and it is on months where the employee has more than one request start and end date.  It shows the data for the first one, but when you come to the days in my calendar that should be highlighted for the second request they don't show.  Not looking for freebie code, but if someone can point me to what part of this I need to adjust to show the second set of request dates in the same month.

 

			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++;
   }

Link to comment
Share on other sites

Here is the full code...

 

 

I have only been hand coding for a few months so please be kind

 

 

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

$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);

//Pull in today's date
$firstmonth = 1;
$lastmonth = 12;

while ($firstmonth <= $lastmonth) { 
$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>";

/*		//Deals with the blank days
	while ( $blank > 0 ) {
		echo "<td class='cal_day_blank'>   </td>";
		$blank = $blank-1;
		$day_count++;
	}
*/
	//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++;

	//start a new row every seven days
	} echo "</tr>";

//Counts the days int he week, up to 7
$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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.