Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Sep 3rd, 2006, 1:22 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,869
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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>
Sane is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PFO Calendar Simulation Sane PHP 0 Apr 15th, 2006 7:57 PM
Can PWS be efficient in Internet applications? sham Other Web Development Languages 1 Nov 19th, 2005 8:12 AM
need help with a javascript calendar rouli JavaScript and Client-Side Browser Scripting 2 Oct 19th, 2005 6:52 AM
Syncronize Calendar iCal with Sync4j server??? hdricard C++ 0 Jul 31st, 2005 10:57 AM
Tdate, calendar and array things gaara Delphi 0 May 13th, 2005 7:56 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:57 AM.

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