PHP Calendar Help

Posted on 2011-03-24
Last Modified: 2012-05-11
Some time ago I wrote a calendar, but the way I wrote it, the week went from Monday - Sunday, but I'd like to change that now to go from Sunday - Saturday.

I wish I could remember my logic behind the calendar, but it was just so long ago.

I know my <th> tag has Sunday first, but the code still displays as if it's monday-sunday, because the 24th comes up as a Wednesday which obviously is incorrect.

<style type="text/css">
  @import url("css/cal-style.css");


if ($permgrpid == '0' or $permgrpid == '')
	header("Location: login.php?error=unauthaccess");
$pgtitl = "  Calendar | Lifting Gear Hire Corporation Intranet";
$titlebarname = "Lifting Gear Hire Corporation Intranet | Calendar";	

$Year = $_GET['year'];
$Month = $_GET['month'];

if ($Month == "") { $Month = date("n"); }
if ($Year == "") { $Year = date("Y"); }
if ($Month==0)  { $Month=12; $Year--; }
if ($Month==13) { $Month=1;  $Year++; }

$Timestamp = strtotime("$Year-$Month-01");

for ($Day=1;$Day<=32;$Day++)
	if (checkdate($Month,$Day,$Year))
		$LastDayInMonth = $Day;

$FirstDayInMonth = date("N",$Timestamp);	

for ($MakeCal=$FirstDayInMonth;$MakeCal<=$FirstDayInMonth+$LastDayInMonth;$MakeCal++)
	//echo "$MakeCal<br />";
$WeeksNeeded = ceil(($FirstDayInMonth+$LastDayInMonth)/7);
echo "<table class='changemonth'> 
			<td colspan='3' align='center'><span class='monthname'>" . date("F Y",$Timestamp) . "</span></td>
			<td class='previousmonth' width='33%'><a href='calendar.php?month=" . (($Month)-1) . "&amp;year=$Year' class='monthview'>Previous Month</a></td>
			<td class='currentmonth' width='34%' align='center'><a href='calendar.php?month=" . date("n") . "&amp;year=" . date("Y") . "' class='monthview'>Current Month</a></td>
			<td class='nextmonth' width='33%'><a href='calendar.php?month=" . (($Month)+1) . "&amp;year=$Year' class='monthview'>Next Month</a></span></td>
echo "<p>";

//Main Table starting with days of week through the month views//

echo "<table cellspacing='0' cellpadding='2' class='cal-table-main'>
<tr class='weekdays'>
	<th width='14%'>Su</th>
	<th width='14%'>M</th>
	<th width='14%'>T</th>
	<th width='14%'>W</th>
	<th width='14%'>Th</th>
	<th width='14%'>F</th>
	<th width='14%'>S</th>

for ($DoWeeks=1;$DoWeeks<=$WeeksNeeded;$DoWeeks++)
	for ($DoDays=1;$DoDays<=7;$DoDays++)
		if ($CurrentDay < $FirstDayInMonth)
			echo "<td width='10%' class='outofrange'>&nbsp;</td>";
			if (($CurrentDay-$FirstDayInMonth)+1>$LastDayInMonth)
				echo "<td width='10%' class='outofrange'>&nbsp;</td>";
				$DayOfYear = date("z",strtotime("$Year-$Month-" . (($CurrentDay-$FirstDayInMonth)+1))) + 1;
				if ($DayOfYear==date("z")+1 && date("Y")==$Year)
					echo "<td width='10%' height='100' valign='top' class='currentday'><strong>" . (($CurrentDay-$FirstDayInMonth)+1) . "</strong><br />";
					echo "<td width='10%' height='100' valign='top'><strong>" . (($CurrentDay-$FirstDayInMonth)+1) . "</strong><br />";
				if ($Event[$DayOfYear]!="")
					echo $Event[$DayOfYear] . $Location[$DayOfYear] . "<p /></font>";
				echo "</td>";
	echo "</tr><tr>";
	echo "</table><p>";

include ("calendar/todaysEvents.php");

Open in new window

Question by:t3chguy

Accepted Solution

jmargraf earned 500 total points
ID: 35209195
While it's hard to see everything that is going on with your code because some of the functions are being pulled in from the include files that aren't visible here, I have an idea on where to start..

In line 36 above, you are determining which day is the first day of the month by this code:  $FirstDayInMonth = date("N",$Timestamp);

the "N" that you are using in the php date function returns a numerical expression of the day of the week, with the number being between 1 and 7 (1 is Monday, and 7 is Sunday).  Since you want the week to start with Sunday, I would first change the code to read this:   $FirstDayInMonth = date("w",$Timestamp);    

changing the "N" to a "w" will change the possible range of numbers returned to 0-6.  In this case, 0 represents Sunday and 6 represents Saturday.  You can see the different parameters you can use with the date function on  at

Also, later in your code, beginning on line 72, you have a for loop where you start looping through the days of the week.  You will probably have to change the loop so that you loop from 0 to 6 (instead of 1 to 7).  Again, this is because date("w") in php returns a value between 0 and 6, with 0 being Sunday.

Author Closing Comment

ID: 35209248
Thank you!!!

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question