Jump to content

issues with highlighting current date in php calendar


kaiman

Recommended Posts

Hi everyone,

 

I have a basic calendar that I am trying to change to highlight the current date. I have got the current date highlighted using an if else statement inside a while clause, however, the issue arises when I use the forward/previous links to go to a different month - the same day is highlighted on those months as well.

 

Here is the code snippet in question (the entire code is below):

 

while( $day_num <= $days_in_month )
{
if(date('d') != $day_num) {
	echo "<td> $day_num </td>";
    	$day_num++;
    	$day_count++;
	}
// display today's date 	 
	else {
   		echo "<td class=\"today\"> $day_num </td>";
   		$day_num++;
$day_count++;
}

 

My question is how do I get this calendar to display the current date, just on the current month. I assume there is something I am missing when it comes to handling the date(s) correctly, but what?

 

Any help is appreciated.

 

Thanks,

 

kaiman

 

Full code:

 

// gets today's date
$date = time () ;

// puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));
$year = (int) ($_GET['year'] ? $_GET['year'] : date('Y'));

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

// get the month name
$current_month = date('F', $first_day) ;

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

// determine blank days before start of month
switch($day_of_week){ 

case "Sun": $blank = 0; break; 

case "Mon": $blank = 1; break; 

case "Tue": $blank = 2; break; 

case "Wed": $blank = 3; break; 

case "Thu": $blank = 4; break; 

case "Fri": $blank = 5; break; 

case "Sat": $blank = 6; break; 

}

// determine how many days are in the current month

$days_in_month = cal_days_in_month(0, $month, $year) ;

// determine next month
$next_month_link = '<a href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'">>></a>';

// determine previous month
$previous_month_link = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'"><<</a>';

// start building the table

echo "	<table border=0 width=294>\n";
echo "		<tr>\n";

// display month and year
echo "			<th> $previous_month_link </th>\n";
echo "			<th colspan=4> $current_month $year </th>\n";
echo "			<th> $next_month_link </th>\n";
echo "				</tr>\n";

// display days of week
echo "				<tr>\n";
echo "					<td width=42>S</td>\n";
echo "					<td width=42>M</td>\n";
echo "					<td width=42>T</td>\n";
echo "					<td width=42>W</td>\n";
echo "					<td width=42>T</td>\n";
echo "					<td width=42>F</td>\n";
echo "					<td width=42>S</td>\n";
echo "				</tr>\n";

// counts the days in the week

$day_count = 1;

echo "				<tr>\n";

// display blank days

while ( $blank > 0 ) 

{ 

echo "					<td></td>\n"; 

$blank = $blank-1; 

$day_count++;

}

// sets the first day of the month to 1 

$day_num = 1;

// count the days in the month and display them

while( $day_num <= $days_in_month )
{
if(date('d') != $day_num) {
	echo "<td> $day_num </td>";
    	$day_num++;
    	$day_count++;
	}
// display today's date 	 
	else {
   		echo "<td class=\"today\"> $day_num </td>";
   		$day_num++;
$day_count++;
}

// make sure we start a new row every week

if ($day_count > 7)

{

echo "				</tr>\n";
echo "				<tr>\n";

$day_count = 1;

}

} 

// finish calendar

while ( $day_count >1 && $day_count <=7 ) 

{ 

echo "				<td>\n";
echo "			</td>\n"; 

$day_count++; 

} 

echo "		</tr>\n";
echo "	</table>\n";

Nevermind, I realized I was comparing two negatives in my if statement and it was always returning positive. Also forgot the month and year details.

 

FYI for anyone who cares - here is my working code:

 

while( $day_num <= $days_in_month ){
if($day_num == date('d') && $month == date('n') && $year == date('Y'))
	{
		// display today's date 
		echo "<td class=\"today\"> $day_num </td>";
    		$day_num++;
    		$day_count++;
		}	 
	else{
	echo "<td> $day_num </td>";    		
   		$day_num++;
$day_count++;
}

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.