Jump to content

[SOLVED] Highlight Today in Calendar


SkyRanger

Recommended Posts

I am trying to figure out how I can highlight todays date in a calendar script.  Anybody have any ideas?

 

  function getEventDays($month, $year) { 
  $days = array(); 
  $sql = mysql_query("
  SELECT DAY(event_date) AS day, COUNT(event_id), event_title 
  FROM calendar_events 
  WHERE MONTH(event_date) = '$month' 
  AND YEAR(event_date) = '$year'
  GROUP BY day"); 



  if (mysql_num_rows($sql) > 0) { 
  while ($row = mysql_fetch_array($sql)) $days[] = $row['day']; 
  } 

  return $days; 
} 

function drawCalendar($month, $year) { 
  // set variables we will need to help with layouts 
  $first       = mktime(0,0,0,$month,1,$year); // timestamp for first of the month 
  $offset      = date('w', $first); // what day of the week we start counting on 
  $daysInMonth = date('t', $first); 
  $monthName   = date('F', $first); 
  $weekDays    = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); 
  $eventDays   = getEventDays($month, $year); 
  
  

  // Start drawing calendar 
  $out  = "<table  border=\"1\" id=\"myCalendar\">\n"; 
  $out .= "<tr><th colspan=\"7\">$monthName $year</th></tr>\n"; 
  $out .= "<tr>\n"; 
  foreach ($weekDays as $wd) $out .= "<td width=\"50\" class=\"weekDays\"><div align=\"center\">$wd</div></td>\n"; 
   
  $i = 0; 
  for ($d = (1 - $offset); $d <= $daysInMonth; $d++) { 
    if ($i % 7 == 0) $out .= "<tr>\n"; // Start new row 
    if ($d < 1) $out .= "<td class=\"nonMonthDay\"> </td>\n"; 
    else { 
      if (in_array($d, $eventDays)) { 
        $out .= "<td class=\"monthDay\">\n"; 
        $out .= "<a href=\"?year=$year&month=$month&day=$d\"><div align=\"center\">$d</div></a>\n"; 
        $out .= "</td>\n";
      } else $out .= "<td class=\"monthDay\"><div align=\"center\">$d</div></td>\n"; 
    } 
    ++$i; // Increment position counter 
    if ($i % 7 == 0) $out .= "</tr>\n"; // End row on the 7th day 
  } 
   
  // Round out last row if we don't have a full week 
  if ($i % 7 != 0) { 
    for ($j = 0; $j < (7 - ($i % 7)); $j++) { 
      $out .= "<td class=\"nonMonthDay\"> </td>\n"; 
    } 
    $out .= "</tr>\n"; 
  } 
   
  $out .= "</table>\n"; 
     
    return $out; 
} 

$year  = isset($_GET['year']) ? $_GET['year'] : date('Y'); 
$month = isset($_GET['month']) ? $_GET['month'] : date('m'); 
$day = isset($_GET['day']) ? $_GET['day'] : date('d');

echo drawCalendar($month, $year); 

// Previous month link
$prevMonth = date('F',strtotime("last month")); 
$prevTS = strtotime("$year-$month-01 -1 month"); // timestamp of the first of last month 
$pMax = date('t', $prevTS); 
$pDay = ($day > $pMax) ? $pMax : $day; 
list($y, $m) = explode('-', date('Y-m', $prevTS)); 
echo "<p>\n"; 
echo "<a href=\"?year=$y&month=$m&day=$pDay\">« $prevMonth</a> |\n";

// Next month link 
$nextMonth = date('F',strtotime("next month")); 
$nextTS = strtotime("$year-$month-01 +1 month"); 
$nMax = date('t', $nextTS); 
$nDay = ($day > $nMax) ? $nMax : $day; 
list($y, $m) = explode('-', date('Y-m', $nextTS)); 
echo "<a href=\"?year=$y&month=$m&day=$nDay\">$nextMonth »</a>\n"; 
echo "</p>\n"; 
?>
</div>

Link to comment
Share on other sites

it seems you need to just do an if statement when displaying each date and in that statement use in_array() to see if the current date you are parsing is in the date array you are using to select your highlighted dates from

 

http://www.php.net/function.in-array

 

if(in_array($CurrentDate, $dateArray)){

  //highlight cell with <div> background color

}

Link to comment
Share on other sites

You can add a new class in your CSS to display todays date as you would like to, then just modify your calendar printing code to check for printing out todays cell.

 

Change:

 

if (in_array($d, $eventDays)) { 
       $out .= "<td class=\"monthDay\">\n"; 
       $out .= "<a href=\"?year=$year&month=$month&day=$d\"><div align=\"center\">$d</div></a>\n"; 
       $out .= "</td>\n";
     } else $out .= "<td class=\"monthDay\"><div align=\"center\">$d</div></td>\n"; 

 

... to this:

 

// Set the current date and the date to be printed
// to compare them
$today = date("m-d-Y");
$current = $month."-".$d."-".$year;

// Check if the current day to be printed is today
if($today == $current){
	$td_class = "monthToday";
} else {
	$td_class = "monthDay";
}

if (in_array($d, $eventDays)) { 
	$out .= "<td class=\"".$td_class."\">\n"; 
	$out .= "<a href=\"?year=$year&month=$month&day=$d\"><div align=\"center\">$d</div></a>\n"; 
	$out .= "</td>\n";
} else $out .= "<td class=\"".$td_class."\"><div align=\"center\">$d</div></td>\n";

Link to comment
Share on other sites

  • 3 months later...

Hi,

i like your calendar script , could you please attache the database schema and all the related calendar files?

add event, delete event , update event, show event, display calendar and any other files you use,

 

more over i wana ask you, does it need to have the user setup his starting day ? becaue my site doesnt need login ..

 

 

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.