Advertisement
Guest User

SO - BusinessHoursCalc

a guest
Feb 18th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class BusinessCalc
  3. {
  4.     const BUSINESS_START = 9;
  5.     const BUSINESS_END = 17;
  6.     const SECONDS_PER_HOUR = 3600;
  7.  
  8.     /**
  9.      * @var array $_excludedDays
  10.      */
  11.     protected $_excludedDays = array(6, 7);
  12.  
  13.     /**
  14.      * @var DateTime $_start
  15.      */
  16.     protected $_start;
  17.  
  18.     /**
  19.      * @param DateTime $start
  20.      */
  21.     public function __construct($start)
  22.     {
  23.         $this->_start = $start;
  24.     }
  25.  
  26.     /**
  27.      * @param DateTime $now
  28.      * @return integer
  29.      */
  30.     public function calculateSeconds(DateTime $now)
  31.     {
  32.         $difference = 0;
  33.         if ($this->_isBusinessDay($this->_start)
  34.             && $this->_isInBusinessTime($this->_start)
  35.         ) {
  36.             $difference += $this->_getEndOfBusinessDay($this->_start)->getTimestamp()
  37.                 - $this->_start->getTimestamp();
  38.         }
  39.  
  40.         if ($this->_isBusinessDay($now)
  41.             && $this->_isInBusinessTime($now)
  42.         ) {
  43.             $difference += $this->_getStartOfBusinessDay($now)->getTimestamp()
  44.                 - $now->getTimestamp();
  45.         }
  46.  
  47.         // the following part looks crappy but works ;)
  48.         // adds business hours to difference
  49.         // checks if the last day is reached, skips this, cause we already have this above
  50.         $dayIterator = $this->_start->getTimestamp();
  51.         while ($dayIterator + 86400 < $now->getTimestamp()
  52.             && $this->_getEndOfBusinessDay(new DateTime('@' . $dayIterator))->getTimestamp() < $now->getTimestamp()
  53.         ) {
  54.             $dayIterator += 86400;
  55.             if ($this->_isBusinessDay(new DateTime('@' . $dayIterator))) {
  56.                 $difference += (self::BUSINESS_END - self::BUSINESS_START) * self::SECONDS_PER_HOUR;
  57.             }
  58.  
  59.         }
  60.  
  61.         return $difference;
  62.     }
  63.  
  64.     /**
  65.      * @param DateTime $time
  66.      * @return DateTime
  67.      */
  68.     protected function _getEndOfBusinessDay($time)
  69.     {
  70.         $endOfDay = new DateTime('@' . $time->getTimestamp());
  71.         $endOfDay->setTime(self::BUSINESS_END, 0, 0);
  72.         return $endOfDay;
  73.     }
  74.  
  75.     /**
  76.      * @param DateTime $time
  77.      * @return DateTime
  78.      */
  79.     protected function _getStartOfBusinessDay($time)
  80.     {
  81.         $startOfDay = new DateTime('@' . $time->getTimestamp());
  82.         $startOfDay->setTime(self::BUSINESS_START, 0, 0);
  83.         return $startOfDay;
  84.     }
  85.  
  86.     /**
  87.      * @param DateTime $time
  88.      * @return integer
  89.      */
  90.     protected function _getWeekDay($time)
  91.     {
  92.         return date('N', $time->getTimestamp());
  93.     }
  94.  
  95.     /**
  96.      * @param DateTime $time
  97.      * @return boolean
  98.      */
  99.     protected function _isInBusinessTime(DateTime $time)
  100.     {
  101.         if ($time->getTimestamp() <= $this->_getEndOfBusinessDay($time)->getTimestamp()
  102.             && $time->getTimestamp() >= $this->_getStartOfBusinessDay($time)->getTimestamp()
  103.         ) {
  104.             return true;
  105.         } else {
  106.             return false;
  107.         }
  108.     }
  109.  
  110.     /**
  111.      * @param DateTime $time
  112.      * @return boolean
  113.      */
  114.     protected function _isBusinessDay($time)
  115.     {
  116.         $weekday = date('N', $time->getTimestamp());
  117.         if (in_array($weekday, $this->_excludedDays) === true) {
  118.             return true;
  119.         } else {
  120.             return false;
  121.         }
  122.     }
  123. }
  124.  
  125.  
  126. error_reporting(-1);
  127. $start = time() - 12234567;
  128. $a = new BusinessCalc(new DateTime('@' . $start));
  129. echo $a->calculateSeconds(new DateTime());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement