• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

determining last business day...

iBrightDev

Jay Street
NLC
NLC
so yeah, i had to create some code to determine what the last business day of the month is each month dynamically so that we could have a link and some text go away for the last business day. this task seemed super easy till they started throwing in things like, if the last business day is on a monday, then make it so the link and text goes away for saturday sunday and monday, making friday the real last business day. then take into consideration, if a holiday is on the last business day (dont think there actually are any). so, this is the crazy opject i built out to get a list of dates that will get the last day of the month, the last business day and any days between so that i can remove the link and text for any of the days found in the array.

PHP:
class lastBusinessDay {
    
    private $today;
    
    public function __construct($today) {
        
		//$today = "2011-10-15";
		
        // IF NO DATE WAS SET, GET TODAYS DATE
        if ($today == null) {
            $this->today         = date("Y-m-d");//"2011-01-25";
        } else {
            $this->today         = $today;
        }
        
		//return $this->today;
		
        //return $this->lastBusinessDay($this->today);
        //return $this->getLastDay($this->today);
        
    }
    
    
    public function getLastDay() {
        
        // GET THE LAST DAY OF THE CURRENT MONTH BASED ON TODAYS DATE
        $dayArray       = $this->findDay($this->today);
        
		//print_r($dayArray);
		
        // GET THE VARIABLES FROM THE LAST DAY OF THE MONTH ARRAY THAT WAS RETURNED
        $day            = $dayArray[0];
        $nDay           = $dayArray[1];
        $yr             = $dayArray[2];
        $month          = $dayArray[3];
        $nMonth         = $dayArray[4];
        
        // LIST OF HOLIDAY DATES
        $newYears       = $this->format_date($yr, 1, 1);         // NEW YEAR'S DAY
        $newYearsO      = $this->observed_day($yr, 1, 1);        // NEW YEAR'S DAY OBSERVED
        $mlk            = $this->get_holiday($yr, 1, 1, 3);      // MARTIN LUTHER KING DAY
        $val            = $this->format_date($yr, 2, 14);        // VALENTINE'S DAY
        $pres           = $this->get_holiday($yr, 2, 1, 3);      // PRESEDENT'S DAY
        $stPatricks     = $this->format_date($yr, 3, 17);        // ST. PATRICK'S DAY
        $easter         = $this->calculate_easter($yr);          // EASTER SUNDAY
        $cdMayo         = $this->format_date($yr, 5, 5);         // CINCO DE MAYO
        $memorial       = $this->get_holiday($yr, 5, 1);         // MEMORIAL DAY
        $independence   = $this->format_date($yr, 7, 4);         // INDEPENDENCE DAY
        $independenceO  = $this->observed_day($yr, 7, 4);        // INDEPENDENCE DAY OBSERVED
        $labor          = $this->get_holiday($yr, 9, 1, 1);      // LABOR DAY
        $columbus       = $this->get_holiday($yr, 10, 1, 2);     // COLUMBUS DAY
        $halloween      = $this->format_date($yr, 10, 31);       // HALLOWEEN
        $veterans       = $this->format_date($yr, 11, 11);       // VETERAN'S DAY
        $thanksgiving   = $this->get_holiday($yr, 11, 4, 4);     // THANKSGIVING
        $christmas      = $this->format_date($yr, 12, 25);       // CHRISTMAS
        
        // OUR OBSERVED HOLIDAYS FOR FREELIFE
        $holidays       = array(
                                $newYearsO,
                                $memorial,
                                $independenceO,
                                $labor,
                                $thanksgiving,
                                $christmas
                            );
        
        // GET THE CURRENT LAST DAY BEING RETURNED
        $lastDay = "{$yr}-{$nMonth}-{$nDay}";
        
        // IF THE LAST DAY OF THE MONTH IS A HOLIDAY, LETS TAKE ONE DAY OFF
        if ( in_array($lastDay, $holidays) ) {
            $nMonth         = $nMonth - 1;
            $nDay           = $nDay - 1;
            if ($day == 'sun') {
                $day    = 'sat';
            } elseif ($day == 'mon') {
                $day    = 'sun';
            } elseif ($day == 'tue') {
                $day    = 'mon';
            } elseif ($day == 'wed') {
                $day    = 'tue';
            } elseif ($day == 'thu') {
                $day    = 'wed';
            } elseif ($day == 'fri') {
                $day    = 'thu';
            } elseif ($day == 'sat') {
                $day    = 'fri';
            }
        }
        
        // NOW WE WILL SEE IF THE LAST DAY IS ON A WEEKEND
		// IF IS IT, WE WILL RESET THE LAST WORKABLE DAY TO FRIDAY
        if ($day == 'sat') {
            $nDay   = $nDay - 1;
        } elseif ($day == 'sun') {
            $nDay   = $nDay - 2;
        } elseif ($day == 'mon') {
            $nDay   = $nDay - 2;
        } else {
            $nDay   = $nDay;
        }
		
		$dayBeforeLast = "{$yr}-{$nMonth}-{$nDay}";
		
		$start  = $dayBeforeLast;
		$end    = $lastDay;
		$dateDiff = $this->date_diff($start,$end);
		//print_r($dateDiff);
		
		$lastWorkDay = $nDay;
		$lastMonthDay = $dayArray[1];
		
		$datesArray = array();
		$datesArray["last_day"]		= $lastDay;
		$datesArray["last_busi"]	= $dayBeforeLast;
		
		if ($lastMonthDay != $lastWorkDay) {
				
				for($i=$lastWorkDay; $i<=$lastMonthDay; $i++) {
						if ($lastWorkDay != $i && $lastMonthDay != $i) {
								$datesArray["id{$i}"]	= "{$yr}-{$nMonth}-{$i}";
						}
				}
				
		}
		
		//print_r($datesArray);
		
        // RETURN WHAT THE LAST WORK DAY HAS BEEN CALCULATED TO
        //return $day . ', ' . $month . ' ' . $nDay . ', ' . $yr;
        return $datesArray;
        
    }
    
    
    // FORMAT THE RETURNED DATE
    private function format_date($year, $month, $day) {
        
        // PAD SINGLE DIGIT MONTH/DAY WITH A LEADING ZERO FOR CONSITENCY
        // AND FORMAT DATE AS DESIRED: YYYY-MM-DD BY DEFAULT
        if (strlen($month) == 1) {
            $month = "0". $month;
        }
        if (strlen($day) == 1) {
            $day = "0". $day;
        }
        
        $today = $year ."-". $month ."-". $day;
        return $today;
    
    }
    
    // FIND THE LAST DAY OF THE MONTH FROM ANY GIVEN DATE
    private function findDay($today=null) {
        
        list($yr,$mn,$dt)   = explode('-',$today);    // separate year, month and date
        $timeStamp          = mktime(0,0,0,$mn,1,$yr);    //Create time stamp of the first day from the give date.
        list($y,$m,$t)      = explode('-',date('Y-m-t',$timeStamp)); //Find the last date of the month and separating it
        $lastDayTimeStamp   = mktime(0,0,0,$m,$t,$y);//create time stamp of the last date of the give month
        $day                = strtolower(date('D',$lastDayTimeStamp)); //date('D',$lastDayTimeStamp);// Find last day of the month
        $nDay               = date('d',$lastDayTimeStamp); // Find last day of the month
        $month              = date('F',$lastDayTimeStamp); // Find last day of the month
        $nMonth             = date('m',$lastDayTimeStamp); // Find last day of the month
        
        return array($day,$nDay,$yr,$month,$nMonth);
    }
    
    
    // IF $WEEK IS NOT PASSED, THEN CHECK FOR LAST WEEK OF MONTH
    private function get_holiday($year, $month, $day_of_week, $week="") {
        
        if ( (($week != "") && (($week > 5) || ($week < 1))) || ($day_of_week > 6) || ($day_of_week < 0) ) {
            // $day_of_week must be between 0 and 6 (Sun=0, ... Sat=6); $week must be between 1 and 5
            return FALSE;
        } else {
            if (!$week || ($week == "")) {
                $lastday = date("t", mktime(0,0,0,$month,1,$year));
                $temp = (date("w",mktime(0,0,0,$month,$lastday,$year)) - $day_of_week) % 7;
            } else {
                $temp = ($day_of_week - date("w",mktime(0,0,0,$month,1,$year))) % 7;
            }
            
            if ($temp < 0) {
                $temp += 7;
            }
            
            if (!$week || ($week == "")) {
                $day = $lastday - $temp;
            } else {
                $day = (7 * $week) - 6 + $temp;
            }
            
            return $this->format_date($year, $month, $day);
        }
        
    }
    
    
    private function observed_day($year, $month, $day) {
        
        // sat -> fri & sun -> mon, any exceptions?
        //
        // should check $lastday for bumping forward and $firstday for bumping back,
        // although New Year's & Easter look to be the only holidays that potentially
        // move to a different month, and both are accounted for.
        
        $dow = date("w", mktime(0, 0, 0, $month, $day, $year));
        
        if ($dow == 0) {
            $dow = $day + 1;
        } elseif ($dow == 6) {
            if (($month == 1) && ($day == 1)) {    // New Year's on a Saturday
                $year--;
                $month = 12;
                $dow = 31;
            } else {
                $dow = $day - 1;
            }
        } else {
            $dow = $day;
        }
        
        return $this->format_date($year, $month, $dow);
        
    }
    
    
    private function calculate_easter($y) {
        
        // In the text below, 'intval($var1/$var2)' represents an integer division neglecting
        // the remainder, while % is division keeping only the remainder. So 30/7=4, and 30%7=2
        //
        // This algorithm is from Practical Astronomy With Your Calculator
        
        $a = $y%19;
        $b = intval($y/100);
        $c = $y%100;
        $d = intval($b/4);
        $e = $b%4;
        $f = intval(($b+8)/25);
        $g = intval(($b-$f+1)/3);
        $h = (19*$a+$b-$d-$g+15)%30;
        $i = intval($c/4);
        $k = $c%4;
        $l = (32+2*$e+2*$i-$h-$k)%7;
        $m = intval(($a+11*$h+22*$l)/451);
        $p = ($h+$l-7*$m+114)%31;
        $EasterMonth = intval(($h+$l-7*$m+114)/31);    // [3 = March, 4 = April]
        $EasterDay = $p+1;    // (day in Easter Month)
        
        return $this->format_date($y, $EasterMonth, $EasterDay);
        
    }
	
	
	// THIS FUNCTION USES THE NATIVE FUNCTIONS OF PHP TO CALCULATE THE DIFFERENCE BETWEEN TWO TIMESTAMPS
	private function date_diff($start,$end = false) {
		
		//echo $start . ' / ' . $end . '<br/>';
		
		$diff = abs(strtotime($end) - strtotime($start));
		
		$years = floor($diff / (365*60*60*24));
		$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
		$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
		
		//printf("%d years, %d months, %d days\n", $years, $months, $days);
		
		return array("year"=>"{$years}", "month"=>"{$months}", "day"=>"{$days}", "start"=>"{$start}", "end"=>"{$end}");
		
	}
	

} // END CLASS LASTBUSINESSDAY

this may be something that could be useful to others for certain things, so, feel free to use the object or parts of it for whatever. :)
 
Back
Top