Date-specific content

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
appleyard
Forum Members
Forum Members
Posts: 27
Joined: Wed Dec 30, 2009 2:54 pm

Date-specific content

Post by appleyard »

I've got a website for a client that will add holiday-specific content and CSS in the weeks before Christmas, Valentine's Day, etc. (for holiday promotions). I haven't seen anything similar in my perusal of the CMSMS forums, so I thought I'd share my crude technique:

Code: Select all

{capture assign="currentmonth"}{$smarty.now|date_format:"%m"}{/capture}
{capture assign="currentday"}{$smarty.now|date_format:"%d"}{/capture}
{if $currentmonth == 12 && $currentday < 25 }
Do Christmas thing
{elseif ( $currentmonth == 1 && $currentday > 28 ) or ( $currentmonth == 2 && $currentday < 15 ) }
Do Valentine thing
{/if}
It's probably not the best way to handle this, so if anyone has a superior method I'd love to hear it. Otherwise, enjoy!
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Date-specific content

Post by calguy1000 »

Nope.

That's prolly the simplest way to handle it.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
appleyard
Forum Members
Forum Members
Posts: 27
Joined: Wed Dec 30, 2009 2:54 pm

Re: Date-specific content

Post by appleyard »

Thanks calguy1000, glad to hear it! I don't suppose you know a way to handle holidays that have shifting dates, do you? (E.g. second Sunday in May, last Thursday in November, etc.) I don't need it for this project, but it might be good for future reference.
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm

Re: Date-specific content

Post by Nullig »

Here is a script to get US holidays, which you may be able to use in a UDT to get the dates you're looking for:

Code: Select all

<?php

/* US Holiday Calculations in PHP
 * Version 1.02
 * by Dan Kaplan <design@abledesign.com>
 * Last Modified: April 15, 2001
 * ------------------------------------------------------------------------
 * The holiday calculations on this page were assembled for
 * use in MyCalendar:  http://abledesign.com/programs/MyCalendar/
 * 
 * USE THIS LIBRARY AT YOUR OWN RISK; no warranties are expressed or
 * implied. You may modify the file however you see fit, so long as
 * you retain this header information and any credits to other sources
 * throughout the file.  If you make any modifications or improvements,
 * please send them via email to Dan Kaplan <design@abledesign.com>.
 * ------------------------------------------------------------------------
*/

// Gregorian Calendar = 1583 or later
if (!$_GET["y"] || ($_GET["y"] < 1583) || ($_GET["y"] > 4099)) {
    $_GET["y"] = date("Y",time());    // use the current year if nothing is specified
}

function format_date($year, $month, $day) {
    // pad single digit months/days with a leading zero for consistency (aesthetics)
    // and format the date as desired: YYYY-MM-DD by default

    if (strlen($month) == 1) {
        $month = "0". $month;
    }
    if (strlen($day) == 1) {
        $day = "0". $day;
    }
    $date = $year ."-". $month ."-". $day;
    return $date;
}

// the following function get_holiday() is based on the work done by
// Marcos J. Montes: http://www.smart.net/~mmontes/ushols.html
//
// if $week is not passed in, then we are checking for the last week of the month
function get_holiday($year, $month, $day_of_week, $week="") {
    if ( (($week != "") && (($week > 5) || ($week < 1))) || ($day_of_week > 6) || ($day_of_week < 0) ) {
        // $day_of_week must be between 0 and 6 (Sun=0, ... Sat=6); $week must be between 1 and 5
        return FALSE;
    } else {
        if (!$week || ($week == "")) {
            $lastday = date("t", mktime(0,0,0,$month,1,$year));
            $temp = (date("w",mktime(0,0,0,$month,$lastday,$year)) - $day_of_week) % 7;
        } else {
            $temp = ($day_of_week - date("w",mktime(0,0,0,$month,1,$year))) % 7;
        }
        
        if ($temp < 0) {
            $temp += 7;
        }

        if (!$week || ($week == "")) {
            $day = $lastday - $temp;
        } else {
            $day = (7 * $week) - 6 + $temp;
        }

        return format_date($year, $month, $day);
    }
}

function observed_day($year, $month, $day) {
    // sat -> fri & sun -> mon, any exceptions?
    //
    // should check $lastday for bumping forward and $firstday for bumping back,
    // although New Year's & Easter look to be the only holidays that potentially
    // move to a different month, and both are accounted for.

    $dow = date("w", mktime(0, 0, 0, $month, $day, $year));
    
    if ($dow == 0) {
        $dow = $day + 1;
    } elseif ($dow == 6) {
        if (($month == 1) && ($day == 1)) {    // New Year's on a Saturday
            $year--;
            $month = 12;
            $dow = 31;
        } else {
            $dow = $day - 1;
        }
    } else {
        $dow = $day;
    }

    return format_date($year, $month, $dow);
}

function calculate_easter($y) {
    // In the text below, 'intval($var1/$var2)' represents an integer division neglecting
    // the remainder, while % is division keeping only the remainder. So 30/7=4, and 30%7=2
    //
    // This algorithm is from Practical Astronomy With Your Calculator, 2nd Edition by Peter
    // Duffett-Smith. It was originally from Butcher's Ecclesiastical Calendar, published in
    // 1876. This algorithm has also been published in the 1922 book General Astronomy by
    // Spencer Jones; in The Journal of the British Astronomical Association (Vol.88, page
    // 91, December 1977); and in Astronomical Algorithms (1991) by Jean Meeus. 

    $a = $y%19;
    $b = intval($y/100);
    $c = $y%100;
    $d = intval($b/4);
    $e = $b%4;
    $f = intval(($b+8)/25);
    $g = intval(($b-$f+1)/3);
    $h = (19*$a+$b-$d-$g+15)%30;
    $i = intval($c/4);
    $k = $c%4;
    $l = (32+2*$e+2*$i-$h-$k)%7;
    $m = intval(($a+11*$h+22*$l)/451);
    $p = ($h+$l-7*$m+114)%31;
    $EasterMonth = intval(($h+$l-7*$m+114)/31);    // [3 = March, 4 = April]
    $EasterDay = $p+1;    // (day in Easter Month)
    
    return format_date($y, $EasterMonth, $EasterDay);
}

/////////////////////////////////////////////////////////////////////////////
// end of calculation functions; place the dates you wish to calculate below
/////////////////////////////////////////////////////////////////////////////

?>

<form action="<?php echo($PHP_SELF); ?>" method="get">
<b>Enter a Year:</b> <input type="text" name="y" value="<?php echo ($_GET["y"]); ?>" size="4" maxlength="4"> <input type="submit" value="Go">
</form>

<?php

// format to use:
//
// get_holiday("year", "month", "day_of_week", "week_of_month");
// get_holiday("year", "month", "day_of_week);    // no 4th field indicates last week of month check
// format_date("year", "month", "day");

echo "<p><b>". $_GET["y"] ." Holidays</b></p>";
echo "<ul>";

echo "<li>New Year's Day = ". format_date($_GET["y"], 1, 1);
    echo "<br>New Year's Day Observed = ". observed_day($_GET["y"], 1, 1);
echo "<li>Martin Luther King Day Observed (Third Monday in January) = ". get_holiday($_GET["y"], 1, 1, 3);
echo "<li>Valentine's Day = ". format_date($_GET["y"], 2, 14);
echo "<li>President's Day Observed (Third Monday in February) = ". get_holiday($_GET["y"], 2, 1, 3);
echo "<li>St. Patrick's Day = ". format_date($_GET["y"], 3, 17);
echo "<li>Easter = ". calculate_easter($_GET["y"]);
echo "<li>Cinco De Mayo = ". format_date($_GET["y"], 5, 5);
echo "<li>Memorial Day Observed (Last Monday in May) = ". get_holiday($_GET["y"], 5, 1);
echo "<li>Independence Day = ". format_date($_GET["y"], 7, 4);
    echo "<br>Independence Day Observed = ". observed_day($_GET["y"], 7, 4);
echo "<li>Labor Day Observed (First Monday in September) = ". get_holiday($_GET["y"], 9, 1, 1);
echo "<li>Columbus Day Observed (Second Monday in October) = ". get_holiday($_GET["y"], 10, 1, 2);
echo "<li>Halloween = ". format_date($_GET["y"], 10, 31);
// Veteran's Day Observed - November 11th ?
echo "<li>Thanksgiving (Fourth Thursday in November) = ". get_holiday($_GET["y"], 11, 4, 4);
echo "<li>Christmas Day = ". format_date($_GET["y"], 12, 25);

echo "</ul>";

?>

Nullig
Post Reply

Return to “Tips and Tricks”