Page 1 of 1

PHP calendar: move the column of Sunday to the end

Posted: Sat Oct 02, 2010 8:15 pm
by lauthiamkok
Hi,

this is the calender script I got it from an online tutorial, it works fine but I want to move the column of Sunday to the end (After the column of Saturday),

Code: Select all

<?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'];
?>

Code: Select all

<?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,

Code: Select all

<?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&year=2010

How can I fix it? or in other words- how can I change the original script so that I can move the Sunday to the end of columns?

thanks!
Lau

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,

Code: Select all

<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 <tr></tr> and there is only a closing </td> but no opening. strange! can I fix it?? thanks!

Re: PHP calendar: move the column of Sunday to the end

Posted: Sat Oct 02, 2010 9:57 pm
by requinix
The problem is actually with the invalid HTML and how it's being generated. That first row has only one <td> when it should have seven; the browser will assume it's in the first column, not the last. If you try last March you'll see it displays correctly.

That single for loop just isn't smart enough to do what it should do.

Code: Select all

// $lastdate is the number of days in the month
// $firstday is the weekday of the first day of the month: 0=Sunday, 6=Saturday
$weekstart = 1; // (Monday) the start of the week, using the same numbering as $firstday

/* the weekday labels go here */


// day numbering

echo "<tr>\n";

// part 1: the first row of the calendar will have empty cells if the first day isn't $weekstart
for ($i = $weekstart; $i != $firstday; $i = ($i + 1) % 7) echo "<td></td>\n";

// part 2: the numbering
for ($i = 1; $i <= $lastdate; $i++) {
    // if it's the week start, begin a row
    if (($i + 11 - $weekstart) % 7 == 0) echo "<tr>\n";

    echo "<td>", $i, "</td>\n";

    // if it's the week end, end the row
    if (($i + 12 - $weekstart) % 7 == 0) echo "</tr>\n";
}

// part 3: the last row will have empty cells if the last day isn't the day before $weekstart
if (($lastdate + $firstday - $weekstart) % 7 != 0) {
    for ($i = $lastdate + $firstday - $weekstart; $i % 7 != 0; $i++) echo "<td></td>\n";
    echo "</tr>\n";
}

Re: PHP calendar: move the column of Sunday to the end

Posted: Sat Oct 02, 2010 10:11 pm
by lauthiamkok
tasairis wrote:The problem is actually with the invalid HTML and how it's being generated. That first row has only one <td> when it should have seven; the browser will assume it's in the first column, not the last. If you try last March you'll see it displays correctly.

That single for loop just isn't smart enough to do what it should do.

Code: Select all

// $lastdate is the number of days in the month
// $firstday is the weekday of the first day of the month: 0=Sunday, 6=Saturday
$weekstart = 1; // (Monday) the start of the week, using the same numbering as $firstday

/* the weekday labels go here */


// day numbering

echo "<tr>\n";

// part 1: the first row of the calendar will have empty cells if the first day isn't $weekstart
for ($i = $weekstart; $i != $firstday; $i = ($i + 1) % 7) echo "<td></td>\n";

// part 2: the numbering
for ($i = 1; $i <= $lastdate; $i++) {
    // if it's the week start, begin a row
    if (($i + 11 - $weekstart) % 7 == 0) echo "<tr>\n";

    echo "<td>", $i, "</td>\n";

    // if it's the week end, end the row
    if (($i + 12 - $weekstart) % 7 == 0) echo "</tr>\n";
}

// part 3: the last row will have empty cells if the last day isn't the day before $weekstart
if (($lastdate + $firstday - $weekstart) % 7 != 0) {
    for ($i = $lastdate + $firstday - $weekstart; $i % 7 != 0; $i++) echo "<td></td>\n";
    echo "</tr>\n";
}
true! the original generates some invalid html! thanks for pointing out!

I have tried your code suggestion, it looks and works fine on this month (Oct), but it become stranger and stranger if you click on to next months,

http://ec-ener.eu/dump/index1.php - it's fine.

http://ec-ener.eu/dump/index1.php?month=11&year=2010 - a problem

thanks.

Re: PHP calendar: move the column of Sunday to the end

Posted: Sat Oct 02, 2010 10:35 pm
by requinix
Oops. Didn't put that bit back in.

Code: Select all

    if (($i + $firstday + 6 - $weekstart) % 7 == 0) echo "<tr>\n";

    echo "<td>", $i, "</td>\n";

    // if it's the week end, end the row
    if (($i + $firstday + 7 - $weekstart) % 7 == 0) echo "</tr>\n";

Re: PHP calendar: move the column of Sunday to the end

Posted: Sat Oct 02, 2010 10:41 pm
by lauthiamkok
tasairis wrote:Oops. Didn't put that bit back in.

Code: Select all

    if (($i + $firstday + 6 - $weekstart) % 7 == 0) echo "<tr>\n";

    echo "<td>", $i, "</td>\n";

    // if it's the week end, end the row
    if (($i + $firstday + 7 - $weekstart) % 7 == 0) echo "</tr>\n";

thanks! I was wondering what the number 11 and number 12 do in these two lines! lol

Code: Select all

// if (($i + 11 - $weekstart) % 7 == 0) echo "<tr>\n";
 //if (($i + 12 - $weekstart) % 7 == 0) echo "</tr>\n";
now it works perfectly! thank you. I am going study your code thorough tomorrow to make sure I fully understand it!

thanks! :D