Jump to content

calendar code shows every month the day in red


clausowitz

Recommended Posts

Hi,

 

I have a calendar code that I found somewhere on the net and adapted to my needs. There's only one thing I don't understand from the original code and that is that CURRDATE() so for today it would be 4 July 2011. Whenever I browse to another month still day 4 is in red, meaning CURRDATE(). I tried but when I change it I get no days in the calendar at all.

 

CODE:

<?php 
print "<link rel='stylesheet' href='cal.css' type='text/css'>";
error_reporting('0'); 

ini_set('display_errors',  '0'); 
// Gather variables from 
// user input and break them 
// down for usage in our script 

if(!isset($_REQUEST['date'])){ 
   $date = mktime(0, 0, 0, date('m'),  date('d'),  date('Y')); 
} else { 
   $date = $_REQUEST['date']; 
} 

$day = date('d',  $date); 
$month = date('m',  $date); 
$year = date('Y',  $date); 

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

// Get friendly month name 
$month_name = date('M',  $month_start); 

// Figure out which day of the week 
// the month starts on. 
$month_start_day = date('D',  $month_start); 

switch($month_start_day){ 
    case "Sun": $offset = 0; break; 
    case "Mon": $offset = 1; break; 
    case "Tue": $offset = 2; break; 
    case "Wed": $offset = 3; break; 
    case "Thu": $offset = 4; break; 
    case "Fri": $offset = 5; break; 
    case "Sat": $offset = 6; break; 
} 

// determine how many days are in the last month. 
if($month == 1){ 
   $num_days_last = cal_days_in_month(0,  12,  ($year -1)); 
} else { 
   $num_days_last = cal_days_in_month(0,  ($month -1),  $year); 
} 
// determine how many days are in the current month. 
$num_days_current = cal_days_in_month(0,  $month,  $year); 

// Build an array for the current days 
// in the month 
for($i = 1; $i <= $num_days_current; $i++){ 
    $num_days_array[] = $i; 
} 

// Build an array for the number of days 
// in last month 
for($i = 1; $i <= $num_days_last; $i++){ 
    $num_days_last_array[] = $i; 
} 

// If the $offset from the starting day of the 
// week happens to be Sunday,  $offset would be 0,  
// so don't need an offset correction. 

if($offset > 0){ 
    $offset_correction = array_slice($num_days_last_array,  -$offset,  $offset); 
    $new_count = array_merge($offset_correction,  $num_days_array); 
    $offset_count = count($offset_correction); 
} 

// The else statement is to prevent building the $offset array. 
else { 
    $offset_count = 0; 
    $new_count = $num_days_array; 
} 

// count how many days we have with the two 
// previous arrays merged together 
$current_num = count($new_count); 

// Since we will have 5 HTML table rows (TR) 
// with 7 table data entries (TD) 
// we need to fill in 35 TDs 
// so,  we will have to figure out 
// how many days to appened to the end 
// of the final array to make it 35 days. 


if($current_num > 35){ 
   $num_weeks = 6; 
   $outset = (42 - $current_num); 
} elseif($current_num < 35){ 
   $num_weeks = 5; 
   $outset = (35 - $current_num); 
} 
if($current_num == 35){ 
   $num_weeks = 5; 
   $outset = 0; 
} 
// Outset Correction 
for($i = 1; $i <= $outset; $i++){ 
   $new_count[] = $i; 
} 

// Now let's "chunk" the $all_days array 
// into weeks. Each week has 7 days 
// so we will array_chunk it into 7 days. 
$weeks = array_chunk($new_count,  7); 


// Build Previous and Next Links 
$previous_link = "<a href=\"".$_SERVER['PHP_SELF']."?date="; 
if($month == 1){ 
   $previous_link .= mktime(0, 0, 0, 12, $day, ($year -1)); 
} else { 
   $previous_link .= mktime(0, 0, 0, ($month -1), $day, $year); 
} 
$previous_link .= "\"><< Prev</a>"; 

$next_link = "<a href=\"".$_SERVER['PHP_SELF']."?date="; 
if($month == 12){ 
   $next_link .= mktime(0, 0, 0, 1, $day, ($year + 1)); 
} else { 
   $next_link .= mktime(0, 0, 0, ($month +1), $day, $year); 
} 
$next_link .= "\">Next >></a>"; 

// Build the heading portion of the calendar table 
echo "<table class=\"maintable\">\n". 
     "<tr class='headline'>\n". 
     "<td colspan=\"7\">\n". 
     "<table align=\"center\">\n". 
     "<tr class='headline'>\n". 
     "<td colspan=\"2\" width=\"125\" align=\"left\">$previous_link</td>\n". 
     "<td colspan=\"3\" width=\"150\" align=\"center\">$month_name $year</td>\n". 
     "<td colspan=\"2\" width=\"75\" align=\"right\">$next_link</td>\n". 
     "</tr>\n". 
     "</table>\n". 
     "</td>\n". 
     "<tr class='mainrow'>\n". 
     "<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>\n". 
     "</tr>\n"; 

// Now we break each key of the array 
// into a week and create a new table row for each 
// week with the days of that week in the table data 

$i = 0; 
while (list(,  $week) = each($weeks))
{

       echo "<tr class='mainrow'>\n"; 
       while (list(,  $d) = each($week)){ 

         if($i < $offset_count){ 
             //$day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0, 0, 0, $month -1, $d, $year)."\">$d</a>"; 
             echo "<td class=\"nonmonthdays\">$day_link</td>\n"; 
         } 
         if(($i >= $offset_count) && ($i < ($num_weeks * 7) - $outset)){ 
            $datecount=mktime(0, 0, 0, $month -1, $d, $year);
         $day_link = $d;
         //$day_link = "<A href=\"javascript:popWin('showcalendaritem.php?ID=$datecount', 350,  400)\">$d</a>";
         $query = "SELECT * FROM bl_calender";
         $r = mysql_query ($query) or die ("Query error: ".mysql_error());
         while($row=mysql_fetch_array($r)) {//Array or records stored in $row
            if($row['dateclass']==$datecount) {
               $short = $row['short'];
               $day_link = "<A href=\"javascript:popWin('showcalendaritem.php?ID=$datecount', 450,  325)\" title=\"$short\"><FONT style='BACKGROUND-COLOR: yellow'>$d</font></a>";}
         }
         $today = mktime(0,0,0,currdate());      
         if($date == mktime(0, 0, 0, $month, $d, $year)){                                       //mktime(0, 0, 0, $month, $d, $year)
            if($today = mktime(0, 0, 0, $month, $d, $year)) {
            echo "<td><font color='red'><b>$d</b></font></td>\n"; 
            } else {
               echo "<td>$d</td>\n";       
            }
           } else { 
               echo "<td class=\"days\">$day_link</td>\n"; 
           } 
        } elseif(($outset > 0)) { 
            if(($i >= ($num_weeks * 7) - $outset)){ 
               $day_link = ""; 
               echo "<td class=\"nonmonthdays\">$day_link</td>\n"; 
           } 
        } 
        $i++; 
      } 
      echo "</tr>\n";    
} 

// Close out your table and that's it! 
echo "<tr class='headline'><td colspan='7' class='days'> </td></tr>"; 
echo '</table>'; 


?> 
<SCRIPT LANGUAGE="Javascript">
        //<!--
        // pop a windoid (Pictures)
        function popWin(url,  w,  h) 
        {
         var madURL = url;
         var x,  y,  winStr;
         x=0; y=0;
         self.name="opener";
         winStr = "height="+h+", width="+w+", screenX="+x+", left="+x+", screenY="+y+", top="+y+", channelmode=0, dependent=0, directories=0, fullscreen=0, location=0, menubar=0, resizable=1, scrollbars=0, status=0, toolbar=0";
         lilBaby = window.open(madURL,  "_blank",  winStr);
        }
        //--> </script>

 

MOD EDIT: code tags added.

Here's where your problem is

 

            if($today = mktime(0, 0, 0, $month, $d, $year)) {
            echo "<td><font color='red'><b>$d</b></font></td>\n"; 
            } else {
               echo "<td>$d</td>\n";       
            }

 

Also, the amount of times you use mktime() and date() is scary. Avoid repeating logic and variables. Don't call a function to find a value if a variable has it stored already.

 

date() and mktime() are slow functions. You can get the same data here

 

$day = date('d',  $date); 
$month = date('m',  $date); 
$year = date('Y',  $date); 

 

In a single call.

list( $day, $month, $year ) = explode( '-', date('d-m-Y',$date) )

 

I know that the code you displayed is the problem but before I had this code and that didn't work, so I tried to change it without luck:

original code.

 

 if($date == mktime(0, 0, 0, $month, $d, $year)){
               echo "<td><A href=\"javascript:popWin('editcalendar.php?
ID=$datecount', 350,  400)\"><font color='red'><b>$d</b></font></a></
td>\n";
           } else {
               echo "<td class=\"days\">$day_link</td>\n";
           } 

 

Marco

I have been struggling with this bit but I cannot get it to work.

 

 if($date == mktime(0, 0, 0, $month, $d, $year)){
               echo "<td><A href=\"javascript:popWin('editcalendar.php?
ID=$datecount', 350,  400)\"><font color='red'><b>$d</b></font></a></
td>\n";
           } else {
               echo "<td class=\"days\">$day_link</td>\n";
           } 

 

Anyone knows what does the trick here?

 

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.