VB icon

Calendar

Email
Submitted on: 1/1/2015 12:32:00 PM
By: Josh Sherman (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: PHP 4.0
Views: 1611
 
     This script displays a calendar (built out of a table) for the specified month and year, and allows you to skip forward and backwards between months. This is a small component of another (larger) project of mine.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :Calendar
//**************************************
* Copyright (C) 2002 Calendar by Josh Sherman <josh@cleancode.org>
 * 
 * This source and program come as is, WITHOUT ANY WARRANTY and/or 
 * WITHOUT ANY IMPLIED WARRANTY.
 * 
 * Users of said software should realize that they cannot and will not hold 
 * myself or Clean Code reliable or responsible for any purpose WHAT SO EVER.
 * Please read all documentation and use said software responsibly.
 *
 * ANY COMMERCIAL REDISTRIBUTION OR ANY PROPRIETARY REDISTRIBUTION OF THIS OR
 * ANY SOURCE FROM CLEANCODE.ORG IS PROHIBITED UNDER CERTAIN CONDITIONS AND
 * SHALL NOT BE RE-SOLD OR REDISTRIBUTED WITHOUT PRIOR AGREEMENTS WITH 
 * CLEANCODE.ORG
 *
 * I can be reached by electronic mail if there are any questions or concerns
 * about this software, or any other software that was written and / or
 * distributed by Cleancode.org <josh@cleancode.org>
 *
 * Software supplied and written by Josh Sherman, http://www.cleancode.org/
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Calendar
// Description:This script displays a calendar (built out of a table) for the specified month and year, and allows you to skip forward and backwards between months. This is a small component of another (larger) project of mine.
// By: Josh Sherman (from psc cd)
//
// Inputs:month and year on the Querystring
//
// Returns:A calendar for the specified month
//
// Side Effects:This code might make you want to be a better person
//**************************************

<?
/* Print out the HTML headers, and calendar header */
echo "<HTML>\n"
 . " <HEAD>\n"
 . " <STYLE>\n"
 . "BODY, TD, TH\n"
 . "{\n"
 . " font-family: Verdana;\n"
 . " font-size: 8pt;\n"
 . "}\n"
 . " </style>\n"
 . " </head>\n"
 . " <BODY>\n";
/* Grab the month and year */
if ($month == "" || $year == "")
 {
 $this_month = date("n");
 $month_name = date("F");
 $this_year = date("Y");
 }
else
 {
 $this_month = date("n", mktime(0, 0, 0, $month, 1, $year));
 $month_name = date("F", mktime(0, 0, 0, $month, 1, $year));
 $this_year = date("Y", mktime(0, 0, 0, $month, 1, $year));
 }
/* This is for the navigation */
$last_month = $this_month - 1;
$next_month = $this_month + 1;
/* Same with all this stuff */
if ($last_month == 12)
 $last_year = $this_year - 1;
else
 $last_year = $this_year;
/* DITTO!!! */
if ($next_month == 1)
 $next_year = $this_year + 1;
else
 $next_year = $this_year;
/* Display the calendar header */
echo " <CENTER><B>Calendar for $month_name, $this_year</b></center><BR>\n" 
 . " <TABLE align=\"center\" border=\"2\" bordercolor=\"#000000\" cellpadding=\"5\" cellspacing=\"0\">\n"
 . "<TR bgcolor=\"#DDDDDD\">\n"
 . "<TH width=\"50\">Sun</th>\n"
 . "<TH width=\"50\">Mon</th>\n"
 . "<TH width=\"50\">Tue</th>\n"
 . "<TH width=\"50\">Wed</th>\n"
 . "<TH width=\"50\">Thu</th>\n"
 . "<TH width=\"50\">Fri</th>\n"
 . "<TH width=\"50\">Sat</th>\n"
 . "</tr>\n";
/* Grab the first day of the month, and total days */
$first_day = date("w", mktime(0, 0, 0, $this_month, 1, $this_year));
$total_days = date("t", mktime(0, 0, 0, $this_month, 1, $this_year));
/* Start on the day of the first week */
$week_num = 1;
$day_num = 1;
/* While the day of the week isn't '7' */
while ($week_num <= 6)
 {
 echo "<TR>\n";
/* Loop through the week days */
for ( $i = 0; $i <= 6; $i++ )
 {
 /* If it's the first week then... */
 if ($week_num == 1)
{
/* If it's not the first day yet, then use a space */
if ($i < $first_day)
 $the_day = " ";
/* If it is the first day, then start with a '1' */
else if ($i == $first_day)
 {
 $the_day = 1;
 }
}
 else
{
/* If we're past the total days, then use spaces */
if ($the_day > $total_days)
 $the_day = " ";
}
 /* Display the day (or space) */
 echo "<TD height=\"50\" width=\"50\" valign=\"top\">$the_day</td>\n";
 /* Incrememnt the day of the month */
 if ($the_day != " ")
$the_day++;
 }
 echo "</tr>\n";
 /* Increment the week number */
 $week_num++;
 }
/* Finish off with closing out our tags */
echo " </table>\n"
 . " <BR>\n"
 . " <CENTER>\n"
 . "<< <A href=\"?month=$last_month&year=$last_year\">Last Month</a> || <A href=\"?month=$next_month&year=$next_year\">Next Month</a> >>\n"
 . " </center>\n"
 . " </body>\n"
 . "</html>\n";
?>


Other 12 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.