My collection of things.

jKafer on the Net

January 21st, 2014 at 10:40 am

A bit of php programming…

I do this type of programming about 4 times a year and each time I have to lookup how to compare dates in php. There are so many options and some make it out to be so complicated I thought I would just thru my answer into the pool. The following code is used to display a message depending on the date. Basically it announces a business will be closed for a holiday.

I use a multidimensional array to hold the holiday information, then go thru it to see if it needs to be displayed. This is also setup so that more than one message could appear at any given time (usually around Christmas & New Years). As you can see from my array, I supplied 2 dates – start date and end date.

array definition: collection = array(start date, end date, first line of message, second line of message)

php code :


$holidays = array(
“Easter”=>array(‘2013-03-20′,’2013-03-30′,’Friday March 29th’,’in recognition of Good Friday’),
“MemDay”=>array(‘2013-05-15′,’2013-05-28′,’Monday May 27th’,’in recognition of Memorial Day’),
“IndDay”=>array(‘2013-06-25′,’2013-07-05′,’Thursday July 4th’,’in recognition of the 4th of July’),
“LabDay”=>array(‘2013-08-15′,’2013-09-03′,’Monday September 2nd’,’in recognition of Labor Day’),
“Christmas”=>array(‘2013-12-10′,’2013-12-26′,’Tues December 24th & Wed, December 25th’,’in recognition of Christmas’),
“NewYear”=>array(‘2013-12-12′,’2014-01-02′,’Wed January 1st’,’in recognition of New Years Day’)

$txtMsg = ‘<p>We will be closed<br /> ‘;
$MsgCount = 0;
$today = date(“Y-m-d”);
foreach ($holidays as $value)

if (date($value[0])<$today && date($value[1])>$today)
$txtMsg .= ‘* <span>’.$value[2].'</span> * <br />’;
$txtMsg .= $value[3].'</p>’;

if ($msgCount >0) {
print $txtMsg;


I could have had the php code write out the day of the week, Month name etc, but because of limited space where this is being placed on the web page, I wanted to control what was displayed. For example, where Christmas is 2 days, writing out the complete names of the days would have caused it to wrap around and look out of place on the web page. But you sure could do that if you wanted to.

I actually have an excel worksheet that has this data in it and does the calculations for me to set the start date 10 days ahead of the shutdown date.


Comments are closed.

  • Pages

  • Latest Pins on Pinterest

    • Follow Me on Pinterest
  • Quilting Projects I Want to Make

    • french braid quilt pattern | Thread: My First French Braid Quilt!

    • quilt kits black and gray - Google Search

    • How To Make A Quillow. One of my aunts made me one as a high school graduation gift. I wore it out and miss it!

    • Cute Cathedral Window by Pitter Putter Stitch. I'm really wanting to try the cathedral window quilt block and this one is so so pretty!

    • Follow Me on Pinterest
  • Categories

  • Latest Pins on Pinterest

    • Crystals Stones: #Crystal Care. More

    • Life path numbers and gemstones, #crystalhealing Believe in numbers

    • Love & Inspiration

    • Now that I am Married to the most incredible, caring, strong, respectful, sexiest guy in the whole world. I now understand my worth. Best Husband in the whole world!!

    • 20 Amazing Quotes About Life