Jump to content

How do I check if a date is a holiday?


funphp

Recommended Posts

Let say we have three holidays in a year, that's New Year Day (01/01), Independence Day (01/04), and Christmas Day (12/25). If a user enters a date, I want to check whether the input date is a holiday or not. Note that the dates are written in the United States format (month/day). I need to use the timestamp to compare the dates.

Link to comment
Share on other sites

Let say we have three holidays in a year, that's New Year Day (01/01), Independence Day (01/04), and Christmas Day (12/25). If a user enters a date, I want to check whether the input date is a holiday or not. Note that the dates are written in the United States format (month/day). I need to use the timestamp to compare the dates.

01/04 should be 07/04

 

Russell's method works with holidays that fall on the same date each year, but you'd need to build the array dynamically for other holidays that fall on a different date each year (e.g. Memorial Day, Easter Monday, Thanksgiving) where you need to know the year.

 

Easter is difficulat because it's based on a lunar calendar, but the easter_days() and easter_date]easter_date() may be useful if you need to work out the date for the Easter holidays, and there's a function in the notes that allows you to work out Easter in the Eastern Orthodox calendar if you need that.

 

Link to comment
Share on other sites

  • 9 months later...

<?php

/* Canadian Holiday Calculations in PHP

* Version 1.00

* by Devin Ryan <r3d_h4t@hotmail.com>

* Last Modified: November 3, 2009

* ------------------------------------------------------------------------

* The holiday calculations on this page were assembled for

* use in a time keeping program for work

*

* 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 Devin Ryan <r3d_h4t@hotmail.com>.

* ------------------------------------------------------------------------

*/

function holiday($date)

{

$year = substr($date, 0, 4);

$holiday = '';

if ($date == $year.'-01-01')

{

$holiday = 'New Years Day';

}

if ($date == date("Y-m-d", strtotime($year.'-02-00, third monday')))

{

$holiday = 'Family Day';

}

if ($date == date("Y-m-d", strtotime("-2 days", (easter_date($year)))))

{

$holiday = 'Good Friday';

}

if ($date == date("Y-m-d", strtotime($year.'-05-25, last monday')))

{

$holiday = 'Victoria Day';

}

if ($date == $year.'-07-01')

{

$holiday = 'Canada Day';

}

if ($date == date("Y-m-d", strtotime($year.'-08-00, first monday')))

{

$holiday = 'Civic Holiday';

}

if ($date == date("Y-m-d", strtotime($year.'-09-00, first monday')))

{

$holiday = 'Labour Day';

}

if ($date == date("Y-m-d", strtotime($year.'-10-00, second monday')))

{

$holiday = 'Thanksgiving Day';

}

if ($date == $year.'-12-25')

{

$holiday = 'Christmas';

}

if ($date == $year.'-12-26')

{

$holiday = 'Boxing Day';

}

return $holiday;

}

if (!$_GET["date"])

{

    $_GET["date"] = date("Y-m-d");

}

$date = $_GET["date"];

echo "<form action=\"\" method=\"get\">

<b>Enter date in format YYYY-MM-DD:</b> <input type=\"text\" name=\"date\" value=\"".$_GET["date"]."\" size=\"10\" maxlength=\"10\">

<input type=\"submit\" value=\"Go\">

</form>";

echo "<h1>".holiday($date)."</h1>";

?>

Link to comment
Share on other sites

I think a switch would be better suited for this, since you would only have one case:

 

<?php 
/* Canadian Holiday Calculations in PHP
* Version 1.00
* by Devin Ryan <r3d_h4t@hotmail.com>
* Last Modified: November 3, 2009
* ------------------------------------------------------------------------
* The holiday calculations on this page were assembled for
* use in a time keeping program for work
* 
* 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 Devin Ryan <r3d_h4t@hotmail.com>.
* ------------------------------------------------------------------------
*/
function holiday($date) {    
  
   $year = substr($date, 0, 4); 
   
   switch($date) {
   
	case $year.'-01-01':
		$holiday = 'New Years Day';
	break;

	case date("Y-m-d", strtotime($year.'-02-00, third monday')):
		$holiday = 'Family Day';
	break;

	case date("Y-m-d", strtotime("-2 days", (easter_date($year)))):
		$holiday = 'Good Friday';
	break;

	case date("Y-m-d", strtotime($year.'-05-25, last monday')):
		$holiday = 'Victoria Day';
	break;

	case $year.'-07-01':
		$holiday = 'Canada Day';
	break;

	case date("Y-m-d", strtotime($year.'-08-00, first monday')):
		$holiday = 'Civic Holiday';
	break;

	case date("Y-m-d", strtotime($year.'-09-00, first monday')):
		$holiday = 'Labour Day';
	break;

	case date("Y-m-d", strtotime($year.'-10-00, second monday')):
		$holiday = 'Thanksgiving Day';
	break;

	case $year.'-12-25':
		$holiday = 'Christmas';
	break;

	case $year.'-12-26':
		$holiday = 'Boxing Day';
	break;

	default:
		$holiday = 'Normal Day';

}

return $holiday;
}
if (!$_GET["date"]) 
   {
    $_GET["date"] = date("Y-m-d");
   }
$date = $_GET["date"];
echo "<form action=\"\" method=\"get\">
<b>Enter date in format YYYY-MM-DD:</b> <input type=\"text\" name=\"date\" value=\"".$_GET["date"]."\" size=\"10\" maxlength=\"10\">
<input type=\"submit\" value=\"Go\">
</form>";
echo "<h1>".holiday($date)."</h1>"; 
?>

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.