Solved

PHP Calendar Help

Posted on 2011-03-24
2
266 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

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 (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  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