Date_Holidays
[ class tree: Date_Holidays ] [ index: Date_Holidays ] [ all elements ]

Source for file USA.php

Documentation is available at USA.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors:   Kevin English <kevin@x5dev.com>                           |
  17. // +----------------------------------------------------------------------+
  18. //
  19.  
  20.  
  21. /**
  22.  * class that calculates observed U.S. holidays
  23.  *
  24.  * @category    Date
  25.  * @package     Date_Holidays
  26.  * @subpackage  Driver
  27.  * @version     $Id: USA.php,v 1.2 2004/11/02 22:53:19 luckec Exp $
  28.  * @author      Kevin English <kevin@x5dev.com>
  29.  */
  30. {
  31.    /**
  32.     * Constructor
  33.     *
  34.     * Use the Date_Holidays::factory() method to construct an object of a certain driver
  35.     *
  36.     * @access   protected
  37.     */
  38.     function Date_Holidays_Driver_USA()
  39.     {
  40.     }
  41.     
  42.    /**
  43.     * Build the internal arrays that contain data about the calculated holidays
  44.     *
  45.     * @access   protected
  46.     * @return   boolean true on success, otherwise a PEAR_ErrorStack object
  47.     * @throws   object PEAR_ErrorStack
  48.     */
  49.     function _buildHolidays()
  50.     {
  51.        /**
  52.         * New Year's Day
  53.         */
  54.         $newYearsDay $this->_calcNearestWorkDay('01','01');
  55.         $this->_addHoliday('newYearsDay'$newYearsDay'New Year\'s Day');
  56.     
  57.  
  58.         $thirdMondayInJanuaryDate  &$this->_calcNthMondayInMonth(1,3);
  59.         $this->_addHoliday('mlkDay'$thirdMondayInJanuaryDate'Dr. Martin Luther King Jr\'s Birthday');
  60.  
  61.        /**
  62.         * President's Day
  63.         */
  64.         $thirdMondayInFebruaryDate  &$this->_calcNthMondayInMonth(2,3);
  65.         $this->_addHoliday('presidentsDay'$thirdMondayInFebruaryDate'President\'s Day');
  66.        /**
  67.         * Memorial Day 
  68.         */
  69.         $lastMondayInMayDate &$this->_calcLastMondayInMonth(5);
  70.         $this->_addHoliday('memorialDay',$lastMondayInMayDate,'Memorial Day');
  71.        /**
  72.         * 4th of July
  73.         */
  74.  
  75.         $independenceDay $this->_calcNearestWorkDay('07','04');
  76.         $this->_addHoliday('independenceDay',$independenceDay,'Independence Day');
  77.  
  78.        /**
  79.         * Labor Day
  80.         */
  81.         $laborDay $this->_calcNthMondayInMonth(9,1);
  82.         $this->_addHoliday('laborDay',$laborDay,'Labor Day');
  83.        /**
  84.         * Columbus Day
  85.         */
  86.         $columbusDay $this->_calcNthMondayInMonth(10,2);
  87.         $this->_addHoliday('columbusDay',$columbusDay,'Columbus Day');
  88.        /**
  89.         * Veteran's  Day
  90.         */
  91.  
  92.         $columbusDay $this->_calcNthMondayInMonth(11,2);
  93.         $this->_addHoliday('veteransDay',$columbusDay,'Veteran\'s Day');
  94.        /**
  95.         * Thanksgiving  Day
  96.         */
  97.  
  98.         $tday$this->_calcNthThursdayInMonth(11,4);
  99.         $this->_addHoliday('thanksgivingDay',$tday,'Thanksgiving Day');
  100.  
  101.        /**
  102.         * Christmas  Day
  103.         */
  104.  
  105.         $tday$this->_calcNearestWorkDay('12','25');
  106.         $this->_addHoliday('christmasDay',$tday,'Christmas Day');
  107.         
  108.         return true;
  109.     }
  110.  
  111.    /**
  112.     * Calculate Nth monday in a month
  113.     * 
  114.     * @access   private
  115.     * @param    int $month      month
  116.     * @param    int $position   position
  117.     * @return   object Date date
  118.     */
  119.     function _calcNthMondayInMonth($month$position{
  120.         if ($position  ==1
  121.           $startday='01';
  122.         elseif ($position==2{
  123.           $startday='08';
  124.         elseif ($position==3{
  125.           $startday='15';
  126.         elseif ($position==4{
  127.           $startday='22';
  128.         elseif ($position==5{
  129.           $startday='29';
  130.         
  131.         $month=sprintf("%02d",$month);
  132.  
  133.         $date   &new Date($this->_year . '-' $month '-' $startday);
  134.         while ($date->getDayOfWeek(!= 1{
  135.             $date  &$date->getNextDay();
  136.         }
  137.         return $date;
  138.     }
  139.  
  140.    /**
  141.     * Calculate Nth thursday in a month
  142.     * 
  143.     * @access   private
  144.     * @param    int $month      month
  145.     * @param    int $position   position
  146.     * @return   object Date date
  147.     */
  148.     function _calcNthThursdayInMonth($month$position{
  149.         if ($position  ==1{
  150.           $startday='01';
  151.         elseif ($position==2{
  152.           $startday='08';
  153.         elseif ($position==3{
  154.           $startday='15';
  155.         elseif ($position==4{
  156.           $startday='22';
  157.         elseif ($position==5{
  158.           $startday='29';
  159.         }
  160.         $month=sprintf("%02d",$month);
  161.                                                                                                                                              
  162.         $date   &new Date($this->_year . '-' $month '-' $startday);
  163.         while ($date->getDayOfWeek(!= 4{
  164.             $date  &$date->getNextDay();
  165.         }
  166.         return $date;
  167.     }
  168.  
  169.    /**
  170.     * Calculate last monday in a month
  171.     * 
  172.     * @access   private
  173.     * @param    int $month  month
  174.     * @return   object Date date
  175.     */
  176.     function _calcLastMondayInMonth($month{
  177.         $month =sprintf("%02d",$month)
  178.         $date   &new Date($this->_year . '-' $month '-01');
  179.         $daysInMonth=$date->getDaysInMonth();
  180.         $date   &new Date($this->_year . '-' $month '-' $daysInMonth );
  181.         while ($date->getDayOfWeek(!= 1{
  182.             $date &$date->getPrevDay();
  183.         }
  184.        
  185.         return $date;
  186.     }
  187.  
  188.    /**
  189.     * Calculate nearest workday for a certain day
  190.     * 
  191.     * @access   private
  192.     * @param    int $month  month
  193.     * @param    int $day    day
  194.     * @return   object Date date
  195.     */
  196.     function _calcNearestWorkDay($month,$day{
  197.         $month =sprintf("%02d",$month)
  198.         $day  =sprintf("%02d",$day);       
  199.       $date   &new Date($this->_year . '-' $month '-' $day)
  200.  
  201.       // When one of these holidays falls on a Saturday, the previous day is also a holiday
  202.       // When New Year's Day, Independence Day, or Christmas Day falls on a Sunday, the next day is also a holiday.
  203.       if ($date->getDayOfWeek(== 0 
  204.         // bump it up one
  205.          $date   &$date->getNextDay();
  206.       
  207.       if ($date->getDayOfWeek(== 6 
  208.         // push it back one
  209.          $date   &$date->getPrevDay();
  210.       
  211.  
  212.       return $date
  213.     
  214.  
  215. }
  216. ?>

Documentation generated on Mon, 11 Mar 2019 14:05:47 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.