funphp Posted January 18, 2009 Share Posted January 18, 2009 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 https://forums.phpfreaks.com/topic/141296-how-do-i-check-if-a-date-is-a-holiday/ Share on other sites More sharing options...
RussellReal Posted January 18, 2009 Share Posted January 18, 2009 an array? <?php $day = date('M-d'); $holidays = array( '01-01', '01-04', '12-25' ); if (in_array($day,$holidays)) // its a holiday!! else // awww :'( ?> Link to comment https://forums.phpfreaks.com/topic/141296-how-do-i-check-if-a-date-is-a-holiday/#findComment-739613 Share on other sites More sharing options...
Mark Baker Posted January 18, 2009 Share Posted January 18, 2009 Quote 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 https://forums.phpfreaks.com/topic/141296-how-do-i-check-if-a-date-is-a-holiday/#findComment-739641 Share on other sites More sharing options...
R3D_H4T Posted November 4, 2009 Share Posted November 4, 2009 <?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 https://forums.phpfreaks.com/topic/141296-how-do-i-check-if-a-date-is-a-holiday/#findComment-950762 Share on other sites More sharing options...
Philip Posted November 4, 2009 Share Posted November 4, 2009 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 https://forums.phpfreaks.com/topic/141296-how-do-i-check-if-a-date-is-a-holiday/#findComment-950765 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.