![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Programming Guru
![]() |
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"> </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> |
|
|
|
|
|
#2 |
|
King of Portal
|
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>
}
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis Last edited by grimpirate; Sep 4th, 2006 at 1:02 AM. |
|
|
|
|
|
#3 |
|
King of Portal
|
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"> </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"> </td>';
}
echo '</tr>';
}
?>
</table>
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
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). |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |