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)
{
$msgCount++;
$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.