Solved

PHP Calendar Help

Posted on 2011-03-24
2
264 Views
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.

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

<?php
require_once($_SERVER['DOCUMENT_ROOT']."/includes/verifyaccess.php");
require_once($_SERVER['DOCUMENT_ROOT']."/includes/mainfunctions.php");

if ($permgrpid == '0' or $permgrpid == '')
	{
	header("Location: login.php?error=unauthaccess");
	die;
	}
$pgtitl = "  Calendar | Lifting Gear Hire Corporation Intranet";
$titlebarname = "Lifting Gear Hire Corporation Intranet | Calendar";	
require_once($_SERVER['DOCUMENT_ROOT']."/includes/header.php");

$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'> 
		<tr>
			<td colspan='3' align='center'><span class='monthname'>" . date("F Y",$Timestamp) . "</span></td>
		</tr>
		<tr>
			<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>
		</tr>
		</table>";
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>
</tr>
";

for ($DoWeeks=1;$DoWeeks<=$WeeksNeeded;$DoWeeks++)
	{
	for ($DoDays=1;$DoDays<=7;$DoDays++)
		{
		$CurrentDay++;
		if ($CurrentDay < $FirstDayInMonth)
			{
			echo "<td width='10%' class='outofrange'>&nbsp;</td>";
			}
		else
			{
			if (($CurrentDay-$FirstDayInMonth)+1>$LastDayInMonth)
				{
				echo "<td width='10%' class='outofrange'>&nbsp;</td>";
				}
			else
				{
				$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 />";
					}
				else
					{
					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

0
Comment
Question by:t3chguy
2 Comments
 
LVL 1

Accepted Solution

by:
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 php.net  at  http://php.net/manual/en/function.date.php

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.
0
 
LVL 1

Author Closing Comment

by:t3chguy
ID: 35209248
Thank you!!!
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

821 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