Jump to content

Holiday Detection Script That Displays Messages A Week Before The Holiday To The Day Of


JesseElser

Recommended Posts

So I wrote this near little script that when a holiday is a week away it displays a custom message on my site counting down to that holiday and then the day of the holiday it changes the message. Works like a charm and I absolutely love how it works, but I realized I made a couple of mistakes and I cannot figure out how to get them fixed. First I'll provide the actual code and then the two issues.

 

<?php

date_default_timezone_set("America/New_York"); // Set to ensure todays date is accurate

$today = date('z',time()); // Get todays date

$nextYear = date("Y" + 1); // Get current year and add one for calculating New Years

//$today = date('z', strtotime('fourth thursday of november')); // date for testing - set to holiday date

 

// Calculate dates for holidays with varying days

$newYearsDay = date('z', strtotime("December 31, " + $nextYear )); // New Years

$valentinesDay = date('z', strtotime("February 14")); // Valentines Day

$memorialDay = date('z', strtotime('last monday of may')); // Memorial Day

$independenceDay = date("z", strtotime("July 4")); // Independence Day

$halloweenDay = date('z', strtotime('October 31')); // Halloween Day

$thanksgivingDay = date('z', strtotime('fourth thursday of november')); // Thanksgiving Day

$christmasDay = date("z", strtotime("December 25")); // Christmas Day

 

// New Years

if($today == $newYearsDay) {

$holidayActive = 1;

$holidayName = "New Years";

$holidaySlogan = "Happy New Year!";

$holidayClass = "independence";

}

else if($newYearsDay - $today <= 7 & $newYearsDay - $today >= 1) {

$holidayActive = 2;

$holidayDate= $newYearsDay;

$holidayName = "New Years";

$holidaySlogan = "Happy New Year!";

$holidayClass = "independence";

}

// Valentines Day

if($today == $valentinesDay) {

$holidayActive = 1;

$holidayName = "Valentines Day";

$holidaySlogan = "Will you be our valentine? ;)";

$holidayClass = "valentines";

}

else if($valentinesDay - $today <= 7 & $valentinesDay - $today >=1) {

$holidayActive = 2;

$holidayDate= $valentinesDay;

$holidayName = "Valentines Day";

$holidaySlogan = "Will you be our valentine? ;)";

$holidayClass = "valentines";

}

// Memorial Day

if($today == $memorialDay) {

$holidayActive = 1;

$holidayName = "Memorial Day";

$holidaySlogan = "You are not forgotten!";

$holidayClass = "memorial";

}

else if($memorialDay - $today <= 7 & $memorialDay - $today >= 1) {

$holidayActive = 2;

$holidayDate= $memorialDay;

$holidayName = "Memorial Day";

$holidaySlogan = "You are not forgotten!";

$holidayClass = "memorial";

}

// Independence Day - Fourth of July

if($today == $independenceDay) {

$holidayActive = 1;

$holidayName = "Independence Day";

$holidaySlogan = "Happy fourth of July!";

$holidayClass = "independence";

}

else if($independenceDay - $today <= 7 & $independenceDay - $today >= 1) {

$holidayActive = 2;

$holidayDate= $independenceDay;

$holidayName = "Independence Day";

$holidaySlogan = "Happy Fourth of July!";

$holidayClass = "independence";

}

// Halloween

if($today == $halloweenDay) {

$holidayActive = 1;

$holidayName = "Halloween";

$holidaySlogan = "Trick or Treat!";

$holidayClass = "halloween";

}

else if($halloweenDay- $today <= 7 & $halloweenDay - $today >= 1) {

$holidayActive = 2;

$holidayDate= $halloweenDay;

$holidayName = "Halloween";

$holidaySlogan = "Trick or Treat";

$holidayClass = "halloween";

}

// Thanksgiving

if($today == $thanksgivingDay) {

$holidayActive = 1;

$holidayName = "Thanksgiving";

$holidaySlogan = "Let's eat some turkey!";

$holidayClass = "thanksgiving";

}

else if($thanksgivingDay - $today <= 7 & $thanksgivingDay - $today >= 1) {

$holidayActive = 2;

$holidayDate= $thanksgivingDay;

$holidayName = "Thanksgiving";

$holidaySlogan = "Let's eat some turkey!";

$holidayClass = "thanksgiving";

}

// Christmas Day

if($today == $christmasDay) {

$holidayActive = 1;

$holidayName = "Christmas Day";

$holidaySlogan = "Merry Christmas!";

$holidayClass = "christmas";

}

else if($christmasDay - $today <= 7 & $christmasDay - $today >= 1) {

$holidayActive = 2;

$holidayDate= $christmasDay;

$holidayName = "Christmas Day";

$holidaySlogan = "Merry Christmas.";

$holidayClass = "christmas";

}

 

if(isset($holidayActive)) {

echo '<div class="notification '. $holidayClass .'">';

if($holidayActive == 1) {

echo 'Today is ' . $holidayName;

echo ' - ' . $holidaySlogan;

}

if($holidayActive == 2) {

$daysRemaining = $holidayDate - $today;

echo "There ";

if($daysRemaining == 1) { echo "is "; } else { echo "are "; }

echo $daysRemaining;

if($daysRemaining == 1) { echo " day ";} else { echo " days "; }

echo "remaining until " . $holidayName . ".";

}

echo '</div>';

}

?>

 

As you can see it is fairely simple. All I'm doing is taking todays date in day of the year format and comparing it to todays day of the year. Works excellent. But heres the issues.

 

For the script to notify that New Years was a week away I had to add one year to the current year. Works fine for the countdown but then I realized that on the day of new years its still going to add that one year to it and not say that "Today is New Years".

 

Here's what I tried.

 

This is the new years section:

 

// New Years

if($today == $newYearsDay) {

$holidayActive = 1;

$holidayName = "New Years";

$holidaySlogan = "Happy New Year!";

$holidayClass = "independence";

}

else if($newYearsDay - $today <= 7 & $newYearsDay - $today >= 1) {

$holidayActive = 2;

$holidayDate= $newYearsDay;

$holidayName = "New Years";

$holidaySlogan = "Happy New Year!";

$holidayClass = "independence";

}

 

 

I figured I could change this line if($today == $newYearsDay) to if($today == 1) and it would work. When I tested it the message did not appear so somewhere I think it is still adding the extra year.

 

What would be the easiest way for calculating the next new years?

 

The second issue is when two holidays are within a week of each other. Halloween is on October 31. Labor Day is November 1st. How could I work around that?

 

I removed Labor Day temporarily until I found a solution. I haven't tried anything new for that issue yet, but thought I would ask anyways.

 

Thanks guys.

 

If you can think of any other possible improvements to this I welcome suggestions.

 

EDIT: If you would like to see it in action I have it live on my site at http://jollyrogerpcs.com if this link is not allowed I will remove it.

Edited by JesseElser
Link to comment
Share on other sites

My first improvement would be to use a db and store the upcoming holidays as dates with attributes for all the things you set for each one.  Then I would take today's date and use the date interval functions to add 7 days to it and then query the db for that date range and grab all the other items stored for that date.  One record per date would be all you need.

 

PS - how do you handle it when two holidays fall inside the same 7 day window?

Link to comment
Share on other sites

My first improvement would be to use a db and store the upcoming holidays as dates with attributes for all the things you set for each one.  Then I would take today's date and use the date interval functions to add 7 days to it and then query the db for that date range and grab all the other items stored for that date.  One record per date would be all you need.

 

PS - how do you handle it when two holidays fall inside the same 7 day window?

I actually haven't figured out two holidays in the same range yet. It's a starter script. I was trying to avoid databases mostly for practical use of copying and pasting to other sites if i wanted to implement the same feature. Good idea though.

Link to comment
Share on other sites

But every year you have to "alter" the code to set your new dates.  With a db-driven script you could expand it to do more than holidays.  You could add your vacation schedule to it.  A birthday list.  A reminder list.  Simply add a date, a name, a greeting, etc. and voila - a reminder pops up.

Link to comment
Share on other sites

I have this one setup to where the year is irrelevent :) For example Thanksgiving is not on a set date but I use this simple line to calculate the date in day of the year format: $thanksgivingDay = date('z', strtotime('fourth thursday of november')); // Thanksgiving Day

 

And I have a seperate system for notifications such as business closures and such.

Link to comment
Share on other sites

This is an example of a function I use for UK holidays, but should be easily adaptable. In UK, if the holiday falls at weekend, the day-off is the following Monday.

function publicHols($yr)
{
    // CALC PUBLIC HOLS FOR $yr
    $hols = array();
    $newyr = "$yr-01-01";
    switch (date('w', strtotime($newyr))) {
        case 6:
            $newyr = "{$yr}-01-03";
            break;
        case 0:
            $newyr = "{$yr}-01-02";
            break;
    }
    $hols['New Year'] = array($newyr,$newyr);
    
    $easter = easter_date($yr);
    $hols['Easter'] = array(date('Y-m-d', strtotime('-2 days', $easter)),
                date('Y-m-d', strtotime('+1 days', $easter)));
                
    $mayday = (new DateTime("first monday of may $yr"))->format('Y-m-d');
    $hols['May Day'] = array($mayday,$mayday);
    
    $sbank = (new DateTime("last monday of may $yr"))->format('Y-m-d');
    $hols['Spring Bank'] = array($sbank,$sbank);
    
    $abank = (new DateTime("last monday of august $yr"))->format('Y-m-d');
    $hols['August Bank'] = array($abank,$abank);
    
    $x1 = "$yr-12-25";
    $x2 = "$yr-12-26";
    switch (date('w', strtotime($x1))) {
        case 5:
            $x2 = "$yr-12-28";
            break;
        case 6:
            $x1 = "$yr-12-27";
            $x2 = "$yr-12-28";
            break;
        case 0:
            $x1 = "$yr-12-26";
            $x2 = "$yr-12-27";
            break;
    }
    $hols['Christmas'] = array($x1,$x2);
    
    return $hols;
}

function showReminders($yr)
{
    $hols = publicHols($yr);
    $today = new DateTime();
    $today->setTime(0,0);
    foreach ($hols as $h=>$dates) {
        $dh = new DateTime($dates[0]);
        $days = $dh->diff($today)->format('%a days');
        if ($dh==$today) {
            echo "It's $h!<br>";
        }
        elseif ($dh > $today && $days <= 7) {
            echo $today->format('jS F Y')." reminder, $h in $days<br>";
        }
    }    
}

$year = date('Y');
showReminders($year);
showReminders($year+1);
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.