Important alert: (current site time 7/15/2013 9:32:25 PM EDT)
 

VB icon

Calendar with Holidays

Email
Submitted on: 5/14/2003 9:20:38 PM
By: Randy McCleary 
Level: Intermediate
User Rating: By 8 Users
Compatibility: 5.0 (all versions)
Views: 16077
(About the author)
 
     This script will display a monthly calendar with the holidays displayed in the day that they fall on. The script will by default display the current month and day, but your can navigate through the months going back and forth and even advance years and so on. I also added a previous script I had posted here on Planet-Source-Code.com to calculate the date that easter falls on.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
=**************************************
= Name: Calendar with Holidays
= Description:This script will display a monthly calendar 
with the holidays displayed in the day that 
they fall on. The script will by default display
the current month and day, but your can 
navigate through the months going back and 
forth and even advance years and so on.
I also added a previous script I had posted
here on Planet-Source-Code.com to calculate the
date that easter falls on.
= By: Randy McCleary
=
= Assumes:Remove the Space in front of 
the <a href=""> tags. The 
site doesn't allow you to 
post it on here.
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=468&lngWId=6=for details.=**************************************

#!/usr/bin/perl -w
###################################################
## Calendar Script V3
## This script will display a monthly calendar 
## with the holidays displayed in the day that 
## they fall on. The script will by default display
## the current month and day, but your can 
## navigate through the months going back and 
## forth and even advance years and so on.
## I also added a previous script I had posted
## here on Planet-Source-Code.com to calculate the
## date that easter falls on.
###################################################
use POSIX;
use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use Time::Local;
###################################################
## Define Globals
###################################################
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = 0;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $month_name= 'jan';
my $time_t=0;
my @attribs = ();
my $start_day=0;
my @month_days = ();
my $days_in_month=0;
my $prev_mon =0;
my $next_mon =0;
my $curr_mon = $mon;
my $prev_year=0;
my $next_year=0;
my $curr_year = $year + 1900;
my ($EasterDay, $EasterMonth);
my ($FirstSun, $FirstMon, $FirstTue, $FirstWed, $FirstThu, $FirstFri, $FirstSat);
my ($day, $TempDays, $LeapDay);
my ($temp_year1, $temp_year2);
$LeapDay = 0;
&init();
&GetFirstWeek();
&Display_Calendar();
###################################################
## Sub to Initiate the calendar
###################################################
sub init {
		print header;
		$year += 1900;
		
		#########################################
		## If the Month and Year Params exist 
		##then get the values
		#########################################
		if (param()) {
			$mon = param('m');
			$year = param('y');
		}
		
		#################################################
		## If Current Mon is Jan then make the previous
		##month December of the previous year
		#################################################
		if ($mon == 0) {
			$prev_mon = 11;
			$prev_year= $year - 1;
		}
		else {
			$prev_mon = $mon - 1;
			$prev_year= $year;
		}
		
		#################################################
		## If Current Mon is Dec then make the Next
		##month Jan of the Next year
		#################################################
		if ($mon == 11) {
			$next_mon = 0;
			$next_year = $year + 1;
		}
		else {
			$next_mon = $mon + 1;
			$next_year= $year;
		}
		$time_t = POSIX::mktime(0, 0, 1, 1, $mon, $year-1900);
		@attribs = localtime($time_t);
		$start_day = @attribs[6] + 1;
		
		@month_days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		$days_in_month = @month_days[$mon];
		$month_name = ("January", "February", "March", 
							"April", "May", "June", "July", 
							"August", "September", "October", 
							"November", "December")[$mon];
		
		#########################
		## Check for leap year
		#########################
		if($mon == 1) {
			if($year%4 == 0 && $year%100 == 0 && $year%400 == 0) {
				$days_in_month = 29;
				$LeapDay = 1;
			} 
			elsif($year%4 == 0 && $year%100 != 0) {
				$days_in_month = 29;
				$LeapDay = 1;
			}
		}		
		
		$days_in_month += $start_day - 1;
		
		#####################################
		### Calculate the date of Easter now
		#####################################
		&CalculateEaster($year);
}
###################################################
## Sub to Diplay the generated Calendar Month
###################################################
sub Display_Calendar {
		$temp_year1 = $year - 1;
		$temp_year2 = $year + 1;
		
		###################################################
		## Begin to Print out the Calendar in a Table
		###################################################
		print "<table cellspacing=\"1\" cellpadding=\"2\" border=\"0\" bgcolor=\"#336699\" align=\"center\" width=\"90%\">\n";
		print " <tr>\n";
		print " <th>< a href=\"?m=$mon&y=$temp_year1\"><font color=\"#FFFFFF\" size=\"1\"><Year</font></a></th>\n";
		print " <th>< a href=\"?m=$prev_mon&y=$prev_year\"><font color=\"#FFFFFF\" size=\"1\"><Month</font></a></th>\n";
		print " <th colspan=\"3\"><font color=\"#FFFFFF\" size=\"3\">$month_name $year</font></th>\n";
		print " <th>< a href=\"?m=$next_mon&y=$next_year\"><font color=\"#FFFFFF\" size=\"1\">Month></font></a></th>\n";
		print " <th>< a href=\"?m=$mon&y=$temp_year2\"><font color=\"#FFFFFF\" size=\"1\">Year></font></a></th>\n";
		print " </tr>\n";
		print " <tr>\n";
		print " <th width=\"14%\" bgcolor=\"#CCCCCC\">Sunday</th>\n";
		print " <th width=\"14%\" bgcolor=\"#CCCCCC\">Monday</th>\n";
		print " <th width=\"14%\" bgcolor=\"#CCCCCC\">Tuesday</th>\n";
		print " <th width=\"14%\" bgcolor=\"#CCCCCC\">Wednesday</th>\n";
		print " <th width=\"14%\" bgcolor=\"#CCCCCC\">Thursday</th>\n";
		print " <th width=\"14%\" bgcolor=\"#CCCCCC\">Friday</th>\n";
		print " <th width=\"14%\" bgcolor=\"#CCCCCC\">Saturday</th>\n";
		print " </tr>\n";
		
		$day = 1;
		my $count = 0;
		my $temp = 0;
		
		for(my $i=1; $i<=$days_in_month; $i++) {
			if($count == 0) { 
				print " <tr>\n"; 
			}
			
			if($count % 7 == 0 && $count != 0) { 
				print " </tr><tr>\n"; 
			}
			
			if($i < $start_day) {
				print " <td bgcolor=\"#EFEFEF\" align=\"center\">-</td>\n";
			} 
			else {
				if($day == $mday && $year == $curr_year && $mon == $curr_mon) {
					print " <td bgcolor=\"#FFFF00\" valign=\"top\" align=\"left\">\n";
					print "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n";
					print " <tr>\n";	
					print "<td align=\"right\"><b>$day</b></td>\n";
					print " </tr>\n";
					print " <tr>\n";
					print "<td align=\"left\"> </td>\n";
					print " </tr>\n";
					print " <tr>\n";
					print "<td align=\"left\"> </td>\n";
					print " </tr>\n";
					print " <tr>\n";
					print "<td align=\"left\"> </td>\n";
					print " </tr>\n";
					print " <tr>\n";
					print "<td align=\"left\"><font size=\"-1\" color=\"blue\"> \n";
					print &CheckForHoliday();
					print " </font>\n";
					print "</td>\n";
					print " </tr>\n";
					print "</table>\n";
				} 
				else {
					print " <td bgcolor=\"#FFFFFF\" valign=\"top\" align=\"left\">\n";
					print "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n";
					print " <tr>\n";	
					print "<td align=\"right\">$day</td>\n";
					print " </tr>\n";
					print " <tr>\n";
					print "<td align=\"left\"> </td>\n";
					print " </tr>\n";
					print " <tr>\n";
					print "<td align=\"left\"> </td>\n";
					print " </tr>\n";
					print " <tr>\n";
					print "<td align=\"left\"> </td>\n";
					print " </tr>\n";
					print " <tr>\n";
					print "<td align=\"left\"><font size=\"-1\" color=\"blue\"> \n";
					print &CheckForHoliday();
					print " </font>\n";
					print "</td>\n";
					print " </tr>\n";
					print "</table>\n";
				}
				
				$day++;
			}
			
			if($i == $days_in_month && $count % 7 != 6 ) {
				$temp = 6 - $count % 7;
				
				for(my $j=1; $j <= $temp; $j++) {
					print " <td bgcolor=\"#EFEFEF\" align=\"center\">-</td>\n";
				}
				
				print " </tr></table>\n";
			}
			
			$count++;
		}
}
#############################################################
## Sub Routine to find the date that easter falls on.
#############################################################
sub CalculateEaster {
	my $Year = shift;
	my ($Century, $G, $K, $I, $J, $L) = 0;
	
	$Century = int $Year / 100;
	$G = $Year % 19;
	$K = int (($Century - 17) / 25);
	$I = ($Century - int ($Century / 4) - int (($Century - $K) / 3) + 19 * $G + 15) % 30;
	$I = $I - (int ($I / 28)) * (1 - (int ($I / 28)) * (int (29 / ($I + 1))) * (int ((21 - $G) / 11)));
	$J = ($Year + int ($Year / 4) + $I + 2 - $Century + int ($Century / 4)) % 7;
	$L = $I - $J;
	
	$EasterMonth = 3 + int (($L + 40) / 44);
	$EasterDay = $L + 28 - 31 * (int ($EasterMonth / 4));	
	
	#Change To Perl Months
	$EasterMonth -= 1;
	
	if ($EasterMonth == 3) {
		$TempDays = 31 + $EasterDay;
	}
	else {
		$TempDays = $EasterDay;
	}
	
	#print $TempDays;
}
#############################################################
## Subroutine to get the first Sunday, Monday, Tuesday
## Wednesday, Thursday, Friday, and Saturday of the month.
## This will be use to find the holidays that occur only 
## on a certain day of the week and always on the same week
## number. 
##Ex: Presidents day is always the 3rd Monday of February.
#############################################################
sub GetFirstWeek {
	#print $start_day;
	
	## If first day of month is Sunday
	if ($start_day == 1) {
		$FirstSun = 1;
		$FirstMon = 2;
		$FirstTue = 3;
		$FirstWed = 4;
		$FirstThu = 5;
		$FirstFri = 6;
		$FirstSat = 7;
	}
	## If first day of month is Monday
	elsif ($start_day == 2) {
		$FirstSun = 7;
		$FirstMon = 1;
		$FirstTue = 2;
		$FirstWed = 3;
		$FirstThu = 4;
		$FirstFri = 5;
		$FirstSat = 6;
	}
	## If first day of month is Tuesday
	elsif ($start_day == 3) {
		$FirstSun = 6;
		$FirstMon = 7;
		$FirstTue = 1;
		$FirstWed = 2;
		$FirstThu = 3;
		$FirstFri = 4;
		$FirstSat = 5;
	}
	## If first day of month is Wednesday
	elsif ($start_day == 4) {
		$FirstSun = 5;
		$FirstMon = 6;
		$FirstTue = 7;
		$FirstWed = 1;
		$FirstThu = 2;
		$FirstFri = 3;
		$FirstSat = 4;
	}
	## If first day of month is Thursday
	elsif ($start_day == 5) {
		$FirstSun = 4;
		$FirstMon = 5;
		$FirstTue = 6;
		$FirstWed = 7;
		$FirstThu = 1;
		$FirstFri = 2;
		$FirstSat = 3;
	}
	## If first day of month is Friday
	elsif ($start_day == 6) {
		$FirstSun = 3;
		$FirstMon = 4;
		$FirstTue = 5;
		$FirstWed = 6;
		$FirstThu = 7;
		$FirstFri = 1;
		$FirstSat = 2;
	}
	## If first day of month is Saturday
	else {
		$FirstSun = 2;
		$FirstMon = 3;
		$FirstTue = 4;
		$FirstWed = 5;
		$FirstThu = 6;
		$FirstFri = 7;
		$FirstSat = 1;
	}
	#print $FirstSun;
}
#############################################################
## Subroutine to check to see if there is a holiday for 
## the date being checked by. More Holidays can be added
## below. Just add what ever you need to check/calculate
## the date that the holiday falls on, like 2nd Monday of
## Jan, etc..
#############################################################
sub CheckForHoliday {	
	my $strHoliday = '';
	
	######################################################
	## New Years, January 1st
	######################################################
	if ($mon == 0 && $day == 1) {
		$strHoliday .= "New Years\n";	
	}
	
	######################################################
	## Martin Luther King Birthday, 3rd Monday in January
	######################################################
	if ($mon == 0 && $day == ($FirstMon + (7*2))) {
		$strHoliday .= "Martin Luther King Birthday\n";
	}
	
	######################################################
	## Groundhog Day, February 2nd
	######################################################
	if ($mon == 1 && $day == 2) {
		$strHoliday .= "Groundhog Day\n";	
	}
	
	######################################################
	## Valentine's Day, February 14th
	######################################################
	if ($mon == 1 && $day == 14) {
		$strHoliday .= "Valentine's Day\n";	
	}
	
	######################################################
	## Presidents Day, 3rd Monday in Februray
	######################################################
	if ($mon == 1 && $day == ($FirstMon + (7*2))) {
		$strHoliday .= "Presidents Day\n";
	}	
	
	######################################################
	## St. Patrick's Day, March 17. 
	######################################################
	if ($mon == 2 && $day == 17) {
		$strHoliday .= "St. Patrick's Day\n";	
	}
	######################################################
	## EU Daylight-Saving Time Begins Last Sunday of March 
	######################################################
	if ($mon == 2 && $day == (31 - (5*$year/4 + 4) % 7)) {
		$strHoliday .= "<font size=\"-1\" color=\"red\">";
		$strHoliday .= "Daylight-Saving Time Begins(EU)</font><br>\n";
	}
		
	######################################################
	## Daylight-Saving Time Begins 1st Sunday of April
	######################################################
	if ($mon == 3 && $day == ($FirstSun)) {
		$strHoliday .= "<font size=\"-1\" color=\"red\">";
		$strHoliday .= "Daylight-Saving Time Begins</font><br>\n";
	}
		
	######################################################
	## April Fools's Day, April 1. 
	######################################################
	if ($mon == 3 && $day == 1) {
		$strHoliday .= "April Fools's Day\n";	
	}
	######################################################
	## Secretaries Day, 4th Wednesday in April
	######################################################
	if ($mon == 3 && $day == ($FirstWed + (7*3))) {
		$strHoliday .= "Secretaries Day\n";
	}
	
	######################################################
	## Ash Wednesday (46 Days before Easter)
	######################################################
	if (((($TempDays - 46) > 0) && ($day == ($TempDays - 46) ) && $mon == 2) || 
		 ((($TempDays - 46) < 0) && ($day == (($TempDays - 46) + 28 + $LeapDay)) && $mon == 1)) {
		$strHoliday .= "Ash Wednesday\n";
	}
	
	######################################################
	## Palm Sunday (Sunday before Easter)
	######################################################
	if (((($TempDays - 7) > 31) && ($day == ($TempDays - 7) - 31) && $mon == 3) || 
		 ((($TempDays - 7) <= 31) && ($day == ($TempDays - 7)) && $mon == 2)) {
		$strHoliday .= "Palm Sunday\n";
	}
	
	######################################################
	## Good Friday (Friday before Easter)
	######################################################
	if (((($TempDays - 2) > 31) && ($day == ($TempDays - 2) - 31) && $mon == 3) || 
		 ((($TempDays - 2) <= 31) && ($day == ($TempDays - 2)) && $mon == 2)) {
		$strHoliday .= "Good Friday\n";
	}
	
	######################################################
	## Easter (Use CalculateEaster)
	######################################################
	if ($mon == $EasterMonth && $day == $EasterDay) {
		$strHoliday .= "Easter Sunday\n";
	}
	
	######################################################
	## Ascension (39 Days After Easter)
	######################################################
	if (((($TempDays - 2) > 31) && ($day == ($TempDays - 2) - 31) && $mon == 3) || 
		 ((($TempDays - 2) <= 31) && ($day == ($TempDays - 2)) && $mon == 2)) {
		#$strHoliday .= "Ascension\n";
	}
	
	######################################################
	## Mother's Day, 2rd Sunday in May
	######################################################
	if ($mon == 4 && $day == ($FirstSun + (7*1))) {
		$strHoliday .= "Mother's Day\n";
	}
	
	######################################################	
	## Armed Forces Day, 3rd Saturday in May
	######################################################
	if ($mon == 4 && $day == ($FirstSat + (7*2))) {
		$strHoliday .= "Armed Forces Day\n";
	}
	
	######################################################
	## Flag Day, June 14th
	######################################################
	if ($mon == 4 && $day == 14) {
		$strHoliday .= "Flag Day\n";	
	}
	
	######################################################
	## Father's Day, 3rd Sunday in June
	######################################################
	if ($mon == 5 && $day == ($FirstSun + (7*2))) {
		$strHoliday .= "Father's Day\n";
	}
	
	######################################################	
	## Independence Day, July 4th
	######################################################
	if ($mon == 6 && $day == 4) {
		$strHoliday .= "Independence Day\n";	
	}
	
	######################################################
	## Parents' Day, 4th Sunday in July
	######################################################
	if ($mon == 6 && $day == ($FirstSun + (7*3))) {
		$strHoliday .= "Parents's Day\n";
	}
	
	######################################################
	## Labor Day, 1st Monday in September
	######################################################
	if ($mon == 8 && $day == $FirstMon) {
		$strHoliday .= "Labor Day\n";
	}
	
	######################################################
	## Grandparents' Day, Sunday after Labor Day
	######################################################
	if ($mon == 8 && $day == $FirstMon + 6) {
		$strHoliday .= "Grandparents' Day\n";
	}
	
	######################################################
	## Daylight-Saving Time Ends - Last Sunday of October
	######################################################
	if ($mon == 9 && $day == (31-($year*5/4+1) % 7) ) {
		$strHoliday .= "<font size=\"-1\" color=\"red\">";
		$strHoliday .= "Daylight-Saving Time Ends</font><br>\n";
	}
	
	######################################################	
	## Columbus Day, 2nd Monday in October
	######################################################
	if ($mon == 9 && $day == ($FirstMon + (7*1))) {
		$strHoliday .= "Columbus Day\n";
	}
	
	######################################################
	## Halloween, October 31
	######################################################
	if ($mon == 9 && $day == 31) {
		$strHoliday .= "Halloween\n";	
	}
	
	######################################################
	## Election Day, Tuesday on or after November 2
	######################################################
	if ($mon == 10 && $day == ($FirstMon + 1)) {
		$strHoliday .= "Election Day\n";	
	}
	
	######################################################
	## Veterans Day, November 11th
	######################################################
	if ($mon == 10 && $day == 11) {
		$strHoliday .= "Veterans Day\n";	
	}
	
	######################################################
	## Thanksgiving Day, 4th Thursday in November
	######################################################
	if ($mon == 10 && $day == ($FirstThu + (7*3))) {
		$strHoliday .= "Thanksgiving Day\n";
	}
	######################################################
	## Perl Harbor Day, December 7th
	######################################################
	if ($mon == 11 && $day == 7) {
		$strHoliday .= "Perl Harbor Day\n";	
	}
	
	######################################################
	## Christmas, December 25th
	######################################################
	if ($mon == 11 && $day == 25) {
		$strHoliday .= "Christmas\n";	
	}
	#-----------------------------------------------------------
	
	return $strHoliday;
}


Other 28 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
5/14/2003 10:09:01 PMBrandon M. Blais

Hey Man,
This Calendar looks great, I saw the screen shots! It looks like you spent a lot of time on this! Awesome! I haven't really viewed the code but from just a quick glance it looks well organized and commented!
Could you take a look at my code if you get a chance, I need some votes, its a file upload script called "see_it2". Good Job!
(If this comment was disrespectful, please report it.)

 
7/2/2003 9:43:32 PMstephen antony

cool work i loved it
(If this comment was disrespectful, please report it.)

 
8/6/2003 10:45:39 AMEl Gato

I apreciate you sharing this. I am using it to schedule stuff with my wife. Our lives have become better to manage with this. Thank you!

(If this comment was disrespectful, please report it.)

 
8/31/2003 10:53:43 PMDon Schumacher

That is great! But, I have a little problem with it...When I execute it I get < a href="?m=7&y=2002"> < a href="?m=6&y=2003"> instead of the << link to change months / years. Any idea why that might be?
(If this comment was disrespectful, please report it.)

 
3/5/2007 8:35:11 AMjay fox

your calendar looks great. thanks for sharing it. its kinda useful for me actually :)
but there's one problem,
When I execute it I get < a href="?m=7&y=2002"> < a href="?m=6&y=2003"> instead of the << link to change months / years
i cant navigate to the other months.
any idea why that happens?

(If this comment was disrespectful, please report it.)

 
3/5/2007 3:56:07 PMRandy McCleary

Hi, all. You have to remove the space before the letter a '< a href'. For some reason PSC doesn't allow you to have code with links like '<a href'.
(If this comment was disrespectful, please report it.)

 

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.