Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Calendar Help

Posted on 2011-03-24
2
Medium Priority
?
273 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

877 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