Advertisement
Guest User

SO - BusinessHoursCalc

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