Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   An Efficient Calendar Generation (http://www.programmingforums.org/showthread.php?t=11246)

Sane Sep 3rd, 2006 2:22 PM

An Efficient Calendar Generation
 
I would like to know if this is a good way to generate a calendar for the given month? If there's anything I can do to dramatically increase the page generation time, or even optimize the HTML?

I'm here to readily learn. :)

Demo : http://saney.ath.cx:8080/nutricraze.php

Code (without stylesheet or javascript *See Demo*):
:

<?php
  // create an "artificial" time, based on an offset, for graceful extending/debugging
  $offset = 0;
  $my_time = time() + $offset;
?>
<h3>Calendar Log - <?php echo date('F Y', $my_time); ?></h3>

<table class="calendar" border="0" cellpadding="0" cellspacing="1">
<tr><th>Sunday</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th></tr>
<?php
  $days = date('t', $my_time);        // days in the month
  $day = date('j', $my_time);        // current day
  $day_of_week = date('w', $my_time); // current "day of week"

  // calculate what "day of week" the month starts on
  $month_start = $day_of_week - $day%7 + 1;
  if ($month_start < 0)
    $month_start = 7 + $month_start;

  $this_day = 0;
  $this_day_of_week = 0;

  // second clause is to ensure the row ends after the days are finished
  while ($this_day < $days || $this_day_of_week != 0)
  {
    // start row
    if ($this_day_of_week == 0)
      echo '<tr>';

    // change day
    if ($this_day == 0)
    {
      // only change to the first day when it's on the "day of week" the month starts on
      if ($this_day_of_week == $month_start)
        $this_day ++;
    }
    else
      $this_day ++;

    // perform output
    // first clause is to create blanks before the first day (0th day)
    // second clause is to create blanks after the month is over
    if ($this_day == 0 || $this_day > $days)
      echo '<td class="na">&nbsp;</td>';
    else if ($this_day == $day)
      echo '<td class="now" onclick="o('.$this_day.')">'.$this_day.'</td>';
    else
      echo '<td onclick="o('.$this_day.')">'.$this_day.'</td>';

    // change "day of week"
    $this_day_of_week ++;

    if ($this_day_of_week == 7)
    {
      // end row
      echo '</tr>';
      $this_day_of_week = 0;
    }
  }
?>
</table>


grimpirate Sep 4th, 2006 1:35 AM

Well, this is gonna be in pseudo code but I'll try and stick to the variables that you put up.
:

month_start = ((7 - day % 7) + day_of_week + 1) % 7
echo <tr>
for(i = 0; i < month_start; i++)
{
        echo <td>blank entry</td>
}
for(i = 1; i <= 7 - month_start; i++)
{
        echo <td>ith entry</td>
}
echo </tr>
tracker = 0        // some variable that I created to keep track of the cyclic day of the week (just a counter really)
for(i = 7 - month_start + 1; i <= days; i++)
{
        if(tracker == 0)
        {
                echo <tr>
        }
        echo <td>ith entry</td>
        tracker++
        if(tracker == 7)
        {
                echo </tr>
                tracker = 0
        }
}
for(i = 0; i < 7 - (days + month_start) % 7; i++)
{
        echo <td>blank entry</td>
}
if((days + month_start) % 7 != 0)
{
        echo </tr>
}

I got no clue if it works though, that's all brain mathematics hehe

grimpirate Sep 5th, 2006 6:28 PM

Well here we go, I reworked it in PHP, with blank entries and the like. I didn't quite understand your formatting for the days of the month and the current day so I leave that up to you. I also stuck to the variables that you used in your code though I sorta used $my_time as a temporary variable.
:

<?
        $offset = 0;
        $my_time = time() + $offset;
?>
<h3>Calendar Log - <? echo date('F Y', $my_time); ?></h3>
<table class="calendar" border="0" cellpadding="0" cellspacing="1">
        <tr>
                <th>Sunday</th>
                <th>Monday</th>
                <th>Tuesday</th>
                <th>Wednesday</th>
                <th>Thursday</th>
                <th>Friday</th>
                <th>Saturday</th>
        </tr>
<?
        $days = date('t', $my_time);
        $day = date('j', $my_time);
        $day_of_week = date('w', $my_time);

        $month_start = (8 - $day % 7 + $day_of_week) % 7;
?>
        <tr>
<?
        for($i = 0; $i < $month_start; $i++)
        {
                echo '<td class="na">&nbsp;</td>';
        }
        for($i = 1; $i <= 7 - $month_start; $i++)
        {
                echo '<td>';
                echo $i;
                echo '</td>';
        }
?>
        </tr>
<?
        $my_time = 0;
        for($i = 8 - $month_start; $i <= $days; $i++)
        {
                if($my_time == 0)
                {
                        echo '<tr>';
                }
                echo '<td>';
                echo $i;
                echo '</td>';
                $my_time++;
                if($my_time == 7)
                {
                        echo '</tr>';
                        $my_time = 0;
                }
        }
        $my_time = ($days + $month_start) % 7;
        if($my_time != 0)
        {
                for($i = 0; $i < 7 - $my_time; $i++)
                {
                        echo '<td class="na">&nbsp;</td>';
                }
                echo '</tr>';
        }
?>
</table>


Sane Sep 5th, 2006 8:36 PM

Ahh, I see. That's definitely the other approach to it. While I was doing it all in one while loop, you break it up since we assume everything will be in a constant form (ABA, persay).

Depending on how quickly PHP handles loops, mine might be faster. But yours probably is. I'll run some tests later on both, and see if I can optimize your method a bit (if it's better).


All times are GMT -5. The time now is 1:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC