Jump to content

Archived

This topic is now archived and is closed to further replies.

sivarts

Maximum Execution Time Exceeded==but WHY?

Recommended Posts

Hi All-

I've been racking my brains over this so I though I would show it to the experts. I am building a simple calendar but when I try and preview in my browser I get the following message:
Fatal error: Maximum execution time of 60 seconds exceeded in C:\htdocs\MYSITE\MYFILE.php on line 147

here's what is around line 147 (line 147 is just a } )
  /* Generate the calendar body */
  while ($currentDay <= $noDays) {
        if ($dayOfWeek == 7) {
              $dayOfWeek = 0;
              $calendar .= "</tr><tr>";
        }
$calendar .= "<td>$currentDay</td>";
[b] } // this is line 147[/b]
  $currentDay++;
  $dayOfWeek++;

/* Filling in the end of the calendar body */
if ($dayOfWeek != 7) { 
          $remainingDays = 7 - $dayOfWeek;
          $calendar .= "<td colspan='$remainingDays'>&nbsp;</td>"; 
}

Can anyone tell my why I might be having this problem?? Here's the complete function:

          <?php
function build_calendar($month,$year,$day) {
/* Declaring the variables */
$daysOfWeek = array('Su','Mo','Tu','We','Th','Fr','Sa');
$firstDayOfMonth = mktime(0,0,0,$month,1,$year);
$noDays = date('t',$firstDayOfMonth);
$dateComponents = getdate($firstDayOfMonth);
$dayOfWeek = $dateComponents['wday'];
$monthName = date('F',mktime(0,0,0,$month,1,$year));
/* Computing the previous month. */
if($month == 1) {
$mn=12;
$yn=$year-1;
} else {
$mn=$month-1;
$yn=$year;
}

/* Computing the next month. */
if($month == 12) {
$mn2=1;
$yn2=$year+1;
} else {
$mn2=$month+1;
$yn2=$year;
}

/* Calendar header: next and previous month links */
$calendar = "<table>";
$calendar .= "<tr><td><a href=day.php?m=$mn&y=$yn&d=$day>&lt;</a></td>";
$calendar .="<td colspan=5 align=center>$monthName, $year</td>";
$calendar .="<td><a href=day.php?m=$mn2&y=$yn2&d=$day>&gt;</a></td></tr>";
$calendar .="<tr>";

/* Calendar header: Display the days of the week */
foreach($daysOfWeek as $day) {
          $calendar .= "<td>$day</td>";
}
$calendar .= "</tr>";
$calendar .= "<tr>";

  $currentDay = 1;


  /* Fill in the beginning of the calendar body */   
  if ($dayOfWeek > 0) { 
    $calendar .= "<td  colspan='$dayOfWeek'>&nbsp;</td>"; 
  }

  /* Generate the calendar body */
  while ($currentDay <= $noDays) {
        if ($dayOfWeek == 7) {
              $dayOfWeek = 0;
              $calendar .= "</tr><tr>";
        }
$calendar .= "<td>$currentDay</td>";
}
  $currentDay++;
  $dayOfWeek++;

/* Filling in the end of the calendar body */
if ($dayOfWeek != 7) { 
          $remainingDays = 7 - $dayOfWeek;
          $calendar .= "<td colspan='$remainingDays'>&nbsp;</td>"; 
}
 
  $calendar .= "</table>";
return $calendar;
}
if (isset($_GET['m']) && isset($_GET['y']) && isset($_GET['d'])){
$month = $_GET['m'];
$year = $_GET['y'];
$day = $_GET['d'];
} else {
$dateComponents = getdate();
$month = $dateComponents['mon'];
$year = $dateComponents['year'];
$day = $dateComponents['mday'];
}

echo build_calendar($month,$year,$day);

?>

Any help would be GREATLY APPRECIATED. I'm a newbie so maybe there is something I am not aware of yet. Thanks!

Share this post


Link to post
Share on other sites
Are you running this off of a shared host? If so, there may be a low execution time, and perhaps your script is taking too long to execute.

Well, I just checked the script on my own server and it is indeed taking too long to load (over 60 seconds). Perhaps you should revise the script to make it so it will not take so long to load.

Share this post


Link to post
Share on other sites
I did some debugging and the problem lies with your while loop.

[code]<?php
while ($currentDay <= $noDays) {
                if ($dayOfWeek == 7) {
                    $dayOfWeek = 0;
                    $calendar .= "</tr><tr>";
                }
                  $calendar .= "<td>$currentDay</td>";
              }
              $currentDay++;
              $dayOfWeek++;
?>[/code]

I tried removing the while($currentDay <= $noDays) and the script loaded fine. You may want to revise that (perhaps use an IF statement instead... although I'm not quite sure what you're doing with the script, so I cannot suggest much).

Share this post


Link to post
Share on other sites
<?php
while ($currentDay <= $noDays) [color=red]{[/color]
                if ($dayOfWeek == 7) {
                    $dayOfWeek = 0;
                    $calendar .= "</tr><tr>";
                }
                  $calendar .= "<td>$currentDay</td>";
              [color=red]}[/color]
              $currentDay++;
              $dayOfWeek++;
?>

The $currentday is incremented OUTSIDE the while loop so you have an infinite loop.

Share this post


Link to post
Share on other sites
Thanks, Barand. I SHOULD have realized that is what was happening. People in this forum rock.

Share this post


Link to post
Share on other sites

×

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.