tags:

views:

141

answers:

2

This is the calendar script I got from an online tutorial. It works fine but I want to move the column of Sunday to the end (after the column of Saturday):

<?php
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"]))  $_REQUEST["year"]  = date("Y");

$month_current = $_REQUEST["month"];
$year_current  = $_REQUEST["year"];

$prev_year = $year_current;
$next_year = $year_current;

$month_previous = $month_current-1;
$month_next = $month_current+1;

if ($month_previous == 0 ) 
{
 $month_previous = 12;
 $prev_year = $year_current - 1;
}

if ($month_next == 13 ) 
{
 $month_next = 1;
 $next_year = $year_current + 1;
}

$timestamp = mktime(0,0,0,$month_current,1,$year_current);
$lastdate    = date("t",$timestamp);

$thismonth = getdate ($timestamp);
$firstday  = $thismonth['wday'];
?>

<?php 
for ($i=0; $i<($lastdate + $firstday); $i++) 
{

 if(($i % 7) == 0 ) echo "<tr>\n";

 if($i < $firstday) echo "<td></td>\n";

 else echo "<td align='center' valign='middle' height='20px'>". ($i - $firstday + 1) . "</td>\n";

 if(($i % 7) == 6 ) echo "</tr>\n";

}
?>

I tried to change the code into this:

<?php 
for ($i=0; $i<($lastdate + $firstday); $i++) 
{

 if(($i % 7) == 1 ) echo "<tr>\n";

 # if $i less than the first day (1), don't print the value of $i
 if($i < $firstday) echo "<td></td>\n";

 # print the value of $i
 else echo "<td align='center' valign='middle' height='20px'>". ($i - $firstday + 1) . "</td>\n";

 if(($i % 7) == 0 ) echo "</tr>\n";

}
?>

It then does not display properly in the column when the first day starts from Sunday. For instance: http://ec-ener.eu/dump/index.php?month=8&amp;year=2010

How can I fix it? Alternatively, how can I change the original script so that I can move Sunday to the end of the columns?

p.s. I also just found out that the original code seems to have a bit problem/ bug, if you check the html - tr and td - it generates,

<tr>
<td align='center' valign='middle' height='20px'>30</td>
<td align='center' valign='middle' height='20px'>31</td>


                              </table>
            </td>
        </tr>

it has the closing table in the and there is only a closing but no opening. I believe that the original single simple loop generates some invalid html! can I fix it?? thanks!

+2  A: 

I think you need to change the firstday variable value for 1st day

$firstday  = $thismonth['wday']; //from here
//adding
$firstday = ($firstday + 6) % 7; //shifting the 1st day
Bang Dao
thanks so much. it works with this line + the original script! :-)
lauthiamkok
you should increase your accept rate by accept this answer ^^
Bang Dao
lol I will! by the way, actually the original simple single loop is misleading - it generates invalid html! u will know what I mean if u read the edited question I posted above. thanks!
lauthiamkok
A: 

There are two problems in your modified script:

The loop is starting from $i = 0, but not generating a <tr> tag until $i = 1. So the first column isn't in any <tr> tag.

Also, if($i < $firstday) needs to be true six times to generate six empty <td></td> tags to move the date to the right column.

To fix it, start the loop from 1, and when Sunday is the first day, set $firstday = 7

<?php 
if($firstday == 0 ) $firstday = 7;
for ($i=1; $i<($lastdate + $firstday); $i++) 
{

  if(($i % 7) == 1 ) echo "<tr>\n";

  # if $i less than the first day (1), don't print the value of $i
  if($i < $firstday) echo "<td></td>\n";

  # print the value of $i
  else echo "<td align='center' valign='middle' height='20px'>". ($i - $firstday + 1) . "</td>\n";

  if(($i % 7) == 0 ) echo "</tr>\n";

}
?>
Lee Reeves
thank you very much. I like this solution as it makes more sense to me bcos I don't quite understand on how how % (modulus division) works! by the way, the end if should be this - if(($i % 7) == 0 ) echo "</tr>\n"; otherwise, it displays oddly... thanks!
lauthiamkok
You're right, I was closing the <tr> tag after printing just one column. Oops. (I'll fix that in my code in case anyone reads this in the future)
Lee Reeves
no problem, actually the original simple single loop is misleading - it generates invalid html!
lauthiamkok
no problem, actually the original simple single loop is misleading - it generates invalid html! u will know what I mean if u read the edited question I posted above. thanks!
lauthiamkok