php monthly content rotator

 

Overview

for some web sites, people may wish to have some static (or dynamic) content that they wish to display on a certain month.

this can even include text, images, videos, javascript, DHTML, etc. basically, any HTML.

code

<?php
$wklen=7;
$d = new DateTime(null, new DateTimeZone("America/Los_Angeles"));
$curyr = intval($d->format('Y')); //current year
$curmo = intval($d->format('m')); //current month
$curday = intval($d->format('d')); //current day
$dow = intval($d->format('w')); //day of week. 0 is sunday
$numdow = intval($curday / $wklen); //will be 2 for 3rd thursday. 0-based number of weeks instance of this day of week above.
$fd = new DateTime(sprintf("%04d-%02d-%02d",$curyr,$curmo,1), new DateTimeZone("America/Los_Angeles"));
$fddow = intval($fd->format('w')); //0 is sunday
$sunday=0;$monday=1;$tuesday=2;$wednesday=3;$thursday=4;$friday=5;$saturday=6;
function findholidayday($dowIn,$wkNumIn) {
	global $curyr,$curmo,$curday,$dow,$numdow,$wklen,$fd,$fddow;
	global $sunday,$monday,$tuesday,$wednesday,$thursday,$friday,$saturday;
	$holidaydowdiff = $dowIn - $fddow;
	if ($fddow > $dowIn) {
		return 1 + $wkNumIn*$wklen + $holidaydowdiff;
	} else {
		return 1 + ($wkNumIn - 1)*$wklen + $holidaydowdiff;
	}
}

//thanksgiving: 4th thursday of November (and by the way, thursday is dow 4)
$thanksgivingdow = $thursday;
$thanksgivingday = findholidayday($thursday,4);

//day of prayer: 1st thursday of May (and by the way, thursday is dow 4)
$prayerdow = $thursday;
$prayerday = findholidayday($thursday,1);

//father's day: 3rd sunday of June (and by the way, sunday is dow 0)
$fathersdow = $sunday;
$fathersday = findholidayday($sunday,3);

//columbus' day: 2nd monday of October (and by the way, monday is dow 1)
$columbusdow = $monday;
$columbusday = findholidayday($monday,2);

//labor day: 1st monday of September (and by the way, monday is dow 1)
$labordow = $monday;
$laborday = findholidayday($monday,1);

switch($curmo) {
	case 1:
		if (1==$curday) {echo "<h2>Happy New Year!</h2>";}
		if ($curday > 1) {echo "<h2>The New Year, ".$curyr." is here!</h2>";}
		echo "<p><img src='/images/snow.jpg'></p>";
		break;
	case 2:
		if ($curday < 14) {echo "<h2>Valentine's Day is on the 14th.</h2>";}
		if (14==$curday) {echo "<h2>Happy Valentine's Day!</h2>";}
		break;
	case 3:
		if (17==$curday) {echo "<h2>Happy St. Patrick's Day!</h2>";}
		break;
	case 4:
		echo "<h2>Good Friday</h2>";
		echo "<p>National Day of Prayer is on May ".$prayerday."</p>";
		break;
	case 5:
		if ($curday < $prayerday) {echo "<p>National Day of Prayer is on May ".$prayerday."</p>";}
		if ($curday == $prayerday) {echo "<p>Today is National Day of Prayer!</p>";}
		echo "<h2>Honor Our Veterans!</h2>";
		break;
	case 6:
		if ($curday < $fathersday) {echo "<h2>Father's Day is on June ".$fathersday."</h2>";}
		if ($curday == $fathersday) {echo "<h2>Today is Father's Day! Happy Father's Day!</h2>";}
		break;
	case 7:
		if ($curday < 4) {echo "<h2>Independence Day is on July 4th!</h2>";}
		if (4 == $curday) {echo "<h2>Happy Independence Day!</h2>";}
		break;
	case 9:
		if ($curday < $laborday) {echo "<h2>Labor Day is on June ".$laborday."</h2>";}
		if ($curday == $laborday) {echo "<h2>Today is Labor Day! Happy Labor Day!</h2>";}
		echo "<h2></h2>";
		echo "<p><img src='/images/autumn.jpg'></p>";
		break;
	case 10:
		if ($curday < $columbusday) {echo "<h2>Columbus Day is on October ".$columbusday."</h2>";}
		if ($curday == $columbusday) {echo "<h2>Happy Columbus Day!</h2>";}
		echo "<p><img src='/images/autumn.jpg'></p>";
		break;
	case 11:
		if ($curday < $thanksgivingday) {echo "<h2>Happy Thanksgiving! (USA November ".$thanksgivingday.")</h2>";}
		if ($curday == $thanksgivingday) {echo "<h2>Today is Thanksgiving Day! Happy Thanksgiving!</h2>";}
		break;
	case 12:
		//occurs every year near Christmas, but give everyone enough notice by doing whole month of December
		if ($curday < 24) {echo "<h2>Christmas is coming December 24th! Merry Christmas!</h2>";}
		if (24 == $curday) {echo "<h2>Christmas Eve! Merry Christmas!</h2>";}
		if (25 == $curday) {echo "<h2>Christmas Day! Merry Christmas!</h2>";}
		if ($curday > 25) {echo "<h2>Merry Christmas and Happy New Year! Happy ".($curyr+1)."!</h2>";}
		echo "<p style='text-align:center;'><a href='http://www.alivingnativity.com/' class='visitorext' target='_blank'>".$curyr." - A Living Nativity Pageant (Vancouver, WA)</a></p>";
		echo "<p><img src='/images/snow.jpg'></p>";
		break;
}
?>