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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

930 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now