Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Jun 3, 2004, 01:39   #1
yjanse
SitePoint Addict
 
yjanse's Avatar
 
Join Date: Apr 2004
Location: The Netherlands
Posts: 240
Exclamation Harry Fuecks Calender code gives an error

I am using the code from PHP Anthology Volume I, Chapter 6.
Everything works fine and groovy, except when I move to the next or previous month. If that month is 31 days instead of 30 I get a E_USER_WARNING: "Calendar::Calendar Day 32 must be a number from 1 to 31"

I doublechecked the code with the book, but I will still include it here.
PHP Code:

<?php 

# -----------------------------------------------------------------
if(!isset($_GET['y'])) $_GET['y'] = date('Y');
if(!isset(
$_GET['m'])) $_GET['m'] = date('m');
if(!isset(
$_GET['d'])) $_GET['d'] = date('d');

$month = new Month($_GET['y'], $_GET['m']);

$last = $month->lastMonth(true);
$next = $month->nextMonth(true);
$thismonth = $month->thisMonth(true);

$cal = "<table class=\"cal\" width=\"420\">\n<tr>\n";
$cal .= "<td colspan=\"2\"> " .
        
"<a class=\"cal_nav\" href=\"". $_SERVER['PHP_SELF'] .
        
"?y=" . date('Y', $last) .
        
"&m=" . date('m', $last) .
        
"&d=1\">" . date('F', $last) . "</a></td>\n";
$cal .= "<td colspan=\"3\" align=\"center\" class=\"cal_now\">" .
        
date('F', $thismonth) . ' ' . date('Y', $thismonth) . "</td>\n";
$cal .= "<td colspan=\"2\" align=\"right\"> " .
        
"<a class=\"cal_nav\" href=\"". $_SERVER['PHP_SELF'] .
        
"?y=" . date('Y', $next) .
        
"&m=" . date('m', $next) .
        
"&d=1\">" . date('F', $next) . "</a></td></tr>\n";

$sDays = array(new Day($_GET['y'], $_GET['m'], $_GET['d']));
$month->buildWeekDays($sDays);

$dagen = array('Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag',
  
'<em>Zaterdag</em>', '<em>Zondag</em>');
$cal .= "<tr class=\"cal_top\">\n";
foreach(
$dagen as $dag) {
  
$cal .= "<th class=\"cal_week\">" . $dag . "</th>";
}
$cal .= "\n</tr>\n";
$alt = '';
while(
$day = $month->fetch()) {
  
$alt = ($alt == "cal_row") ? "cal_row_alt" : "cal_row";
  if(
$day->isFirst()) {
    
$cal .= "<tr class=\"" . $alt . "\">\n";
  }
  if(!
$day->isEmpty()) {
    if(!
$day->isSelected()) {
      
$cal .= "<td>";
    } else {
      
$cal .= "<td class=\"cal_current\">";
    }
    
$cal .= "<a class=\"cal_entry\" href=\"" . $_SERVER['PHP_SELF'] .
        
"?y=" . $day->thisYear() .
        
"&m=" . $day->thisMonth() .
        
"&d=" . $day->thisDay() . "\">" . $day->thisDay() . "</a></td>";
  } else {
    
$cal .= "<td class=\"cal_entry\">&nbsp;</a>";
  }
  if(
$day->isLast()) {
    
$cal .= "\n</tr>\n";
  }
}
$cal .= "</table>\n";
echo
'<p></p>' . $cal . '<p></p>';
# -----------------------------------------------------------------
?>

Last edited by yjanse; Jun 3, 2004 at 03:05.
yjanse is offline   Reply With Quote
Old Jun 3, 2004, 01:41   #2
yjanse
SitePoint Addict
 
yjanse's Avatar
 
Join Date: Apr 2004
Location: The Netherlands
Posts: 240
The class files from the SPLIB package are unedited and included. (Otherwise it wouldn't work at all).

Also, after showing the warning, the entire calendar is still rendered without errors.
yjanse is offline   Reply With Quote
Old Jun 3, 2004, 07:04   #3
ehando
SitePoint Member
 
Join Date: May 2004
Location: Brisbane
Posts: 19
I think that the calendar library evolved into PEAR::Calendar. Check it out at the pear repository OR do a search for info on these forums. I've got a forum thread happening over in the advanced php section that involves pear::calendar, so you might want to check that out as well.
ehando is offline   Reply With Quote
Old Jun 3, 2004, 07:08   #4
yjanse
SitePoint Addict
 
yjanse's Avatar
 
Join Date: Apr 2004
Location: The Netherlands
Posts: 240
Thanks for the tips, I will look further.
yjanse is offline   Reply With Quote
Old Jun 4, 2004, 16:09   #5
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,906
That's right - I'm ashamed to say there does seem to a bug that I missed in the version of the Calendar library in the book. Performance-wise, that code could also be better.

That's largely the reason it evolved into PEAR::Calendar, which has come on a long way since then. Using it is still similar - there's documentation here: http://pear.php.net/manual/en/package.datetime.php which shows how it works. You may want to read Getting Started with PEAR if you're unsure how to install it
HarryF is offline   Reply With Quote
Old Jun 4, 2004, 16:16   #6
mrWoot
SitePoint Zealot
 
mrWoot's Avatar
 
Join Date: Jan 2004
Location: Wisconsin
Posts: 132
I say we all burn the book :P (kidding, nice work on the book)
mrWoot is offline   Reply With Quote
Old Jun 4, 2004, 23:56   #7
yjanse
SitePoint Addict
 
yjanse's Avatar
 
Join Date: Apr 2004
Location: The Netherlands
Posts: 240
Harry, first: thanks for the great book.
I have never seen a programmingbook without errors in the code yet, so nothing to be ashamed of.
I am already using PEAR for validation, but not much else. I believe that I want to be able to solve every problem myself, and then when I am confident about my skills I will use PEAR and Eclipse and the likes. I just want to know what every piece of the code does
To be honest, I chose to use the calender code from your book because it looked simpler than the PEAR::Calender, that overwhelmed me a bit. I will look into it now, and see if I can bushwhack through it, and find the few basic things I need.

Btw:
The problem I have to solve with calenders is this:
For an online bookingsystem for a learning center I want to show a calender per month, where every day is colored with a color:

Green = Opened and there is still room
Orange = Opened but no room left
White = Closed
Gray = Weekends and holidays
[/ul]

I was breaking my brain on how I could add that to your code, because I have to perform multiple calculations based on many factors for it, using SQL and several functions I wrote. Maybe I have to write a function like isFirst() in your class which handles all of that, and call it "isRoomLeft()" or something.
yjanse is offline   Reply With Quote
Old Aug 2, 2004, 00:56   #8
desertlynx
SitePoint Member
 
desertlynx's Avatar
 
Join Date: Aug 2004
Location: RiO
Posts: 3
Post

I started reading the book, PHP Anthology vol.1, lastweek and now I've reached chapter 6. So far it's a really great book and I want to thank Harry for providing a great start in learning about the OOP techniques with PHP.

Anyways, I also got the same error message when I executed the roman calendar script. Though it was really a hard task for me novice OOP programmer to track down the bug, after spending considarable time analyzing the source I managed to find what is the problem. I think what causes the error is the line 215 in 'Month.php'.

(Month.php)
PHP Code:

213: if ( !$end && $dayOfMonth == $this->dateMath->daysInMonth()) { 

214:       $end = true;
215:       $dayOfMonth++;
216: } else if ( $end ) {
I presume that after the value of variable "$end" turned true, there is no need to increment the value of variable "$dayOfMonth" in the next line. After removing the line, the code ceases to produce the error message, "Calendar::Calendar Day 32 must be a number from 1 to 31", and still works fine.

However pls note that, as stated above, I'm by no means an OOP guru or a php master, so maybe my finding is incorrect and ridiculous... well, in that case just ignore my post.

P.S. I've just downloaded the PEAR::Calendar and am viewing the code. It's very interesting and great. But, like yjanse's saying, the PEAR::Calender seems a bit complex and it overwhelmes me too. Anyways, if I have an oppotunity to build a site that needs a neat calendar system, I will challenge to use the PEAR::Calendar for the better performance and stability.


Addenda:
OK, I noticed that the above modification(removing the line 215) affects the highlight system of the roman calendar script. I mean when you click the last day or (last day-1) of a month, the highlight system seems not to work correctly. To fix this problem, you have to add one new line of code (000) before the line 229 and then you also need to change the line 232 of Month.php as follows.

Well, I do know this is not a brilliant solution, but at the same time this is not that bad, I think. You know, the only thing you have to do here is to write the 2 line of code, anyways.

(Month.php)
PHP Code:

000: $theDay = $end ? $dayOfMonth : $dayOfMonth-1; // need to add this line

229: foreach ( $sDates as $sDate ) {
230:    if ( $this->year == $sDate->thisYear()
231:                   && $this->month == $sDate->thisMonth()
232:                          && $theDay == $sDate->thisDay() // need to change this line
233:                                && !$this->children[$i]->isEmpty() )
234:                    $this->children[$i]->setSelected();
235:     }

(Sorry for my bad english. I hope you guys understand what I'm saying)

Last edited by desertlynx; Aug 2, 2004 at 21:09.
desertlynx is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 18:04.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved