FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

PHP Calendar Bug

 


asiddle423
Hi
I have a calendar code that isn't returning the 1st sunday onto the next line if it isn't the first day of the month can anyone help the code is as follows.

Code:
<?php
//Set calendar info
$currentDay = date('d');
$currentMonth = date('m');
$currentYear = date('Y');
$currentDayName = date('l');
$currentDayS = date('dS');
$currentMonthName = date('F');

//Get the number of days in current month
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear);

//Get offset of 1st day
$firstDayOfMonth = date('w', mktime(0, 0, 0, $currentMonth, 1, $currentYear));

//Set the names of the days in the week
$days = array();
$days[] = "Sunday";
$days[] = "Monday";
$days[] = "Tuesday";
$days[] = "Wednesday";
$days[] = "Thursday";
$days[] = "Friday";
$days[] = "Saturday";

//Display all the day names
$dayName = "";
foreach($days as $x => $y)
{
   $dayName .= "<td align='center' bgcolor='#999999' width='14.28%' >".$y." </td>";
}

//Spacers for the offset for the 1st day of the month
$weekDays = "";
$i = $fisrtDayOfMonth + 1;
if($firstDayOfMonth != "0")
{
   $weekDays .= "<td colspan='".$firstDayOfMonth."'> </td>";
}

//Set default first day to 1
$iDay = 1;
$ii = $i;
$amountOfDays = $daysInMonth + $firstDayOfMonth;
for($i; $i <= $amountOfDays; $i++)
{
   //$i decides what the row colour will be
   if($i % 2)
   {
      $color = '#ffaaff';
   }
   else
   {
      $color = '#ffffaa';
   }
   
   //If the current day is sunday, make sure a new row starts
   if($ii == 8)
   {
      $weekDays .= "</tr><tr>";
      $ii = 1;
   }
   
   //Highlight the current day
   if($currentDay == $iDay)
   {
      $extra = 'bgcolor="#ff0000"';
   }
   else
   {
      $extra = 'bgcolor="#'.$color.'"';
   }
   
   //Show the days
   $weekDays .= "<td height='65' valign='top' ". $extra .">". $iDay ."</td>";
   
   //increment the day and week number (ii)
   $iDay++;
   $ii++;
}

//Add end month spacers
if((8 - $ii) >= "1")
{
   $weekDays .= "<td colspan='". (8-$ii) ."'></td>";
}
?>

<html>
<style type="text/css">
table,tr,td,th,tbody,TR,TD
{
    font-family: Verdana;
    font-size: 8pt;
    font-size: 11; color: #000000;
    border-font-size: 11; color: #000000;
    border-collapse: collapse;
}
</style>
<table border="1" cellpadding="1" cellspacing="1" width="98%">
   <tr>
      <td align="center" colspan="7" bgcolor="#999999">
         <?php echo $currentDayName ." the ". $currentDayS ." of ". $currentMonth .", ". $currentYear; ?>
      </td>
   </tr>
   <tr>
      <?php echo $dayName; ?>
   </tr>
   <tr>
      <?php echo $weekDays; ?>
   </tr>
</table>
</html>
nikolic
I just took a quick look at your script. I haven't looked at the logic in the code but was looking for syntax issues. The way that the array is populated is odd to me. Try explicitly setting the indexes. This can be done in a number of ways, but here is one:

Change the following:

Code:

//Set the names of the days in the week
$days = array();
$days[] = "Sunday";
$days[] = "Monday";
$days[] = "Tuesday";
$days[] = "Wednesday";
$days[] = "Thursday";
$days[] = "Friday";
$days[] = "Saturday";


to:

Code:
//Set the names of the days in the week
$days = array(
                      "Sunday",
                      "Monday",
                      "Tuesday",
                      "Wednesday",
                      "Thursday",
                      "Friday",
                      "Saturday"
                      );


and also be sure to avoid the "off by one" error. Any error involving only one index of an array should be checked to be sure if you are starting your logic at 0 or 1. And of course, arrays start their indexes at 0.
asiddle423
Thanks will change that and remember that, I understand that the index starts at 0

Thx
Asiddle423
BlackSkad
One little typo can ruine your complete script:
Code:
$i = $fisrtDayOfMonth + 1;

should become
Code:
$i = $firstDayOfMonth + 1;


It considered $fisrtDayOfMonth being zero, and thus $i was always 1.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.