Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class BusinessCalc
- {
- const BUSINESS_START = 9;
- const BUSINESS_END = 17;
- const SECONDS_PER_HOUR = 3600;
- const SECONDS_PER_DAY = 86400;
- /**
- * @var array $_excludedDays
- */
- protected $_excludedDays = array(6, 7);
- /**
- * @var DateTime $_start
- */
- protected $_start;
- /**
- * @param DateTime $start
- */
- public function __construct($start)
- {
- $this->_start = $start;
- }
- /**
- * @param DateTime $now
- * @return integer
- */
- public function calculateSeconds(DateTime $now)
- {
- $difference = 0;
- if ($this->_isBusinessDay($this->_start)
- && $this->_isInBusinessTime($this->_start)
- ) {
- $difference += $this->_getEndOfBusinessDay($this->_start)->getTimestamp()
- - $this->_start->getTimestamp();
- }
- if ($this->_isBusinessDay($now)
- && $this->_isInBusinessTime($now)
- ) {
- $difference += $this->_getStartOfBusinessDay($now)->getTimestamp()
- - $now->getTimestamp();
- }
- // the following part looks crappy but works ;)
- // adds business hours to difference
- // checks if the last day is reached, skips this, cause we already have this above
- $dayIterator = $this->_start->getTimestamp();
- while ($dayIterator + self::SECONDS_PER_DAY < $now->getTimestamp()
- && $this->_getEndOfBusinessDay(new DateTime('@' . $dayIterator))->getTimestamp() < $now->getTimestamp()
- ) {
- $dayIterator += self::SECONDS_PER_DAY;
- if ($this->_isBusinessDay(new DateTime('@' . $dayIterator))) {
- $difference += (self::BUSINESS_END - self::BUSINESS_START) * self::SECONDS_PER_HOUR;
- }
- }
- return $difference;
- }
- /**
- * @param DateTime $time
- * @return DateTime
- */
- protected function _getEndOfBusinessDay($time)
- {
- $endOfDay = new DateTime('@' . $time->getTimestamp());
- $endOfDay->setTime(self::BUSINESS_END, 0, 0);
- return $endOfDay;
- }
- /**
- * @param DateTime $time
- * @return DateTime
- */
- protected function _getStartOfBusinessDay($time)
- {
- $startOfDay = new DateTime('@' . $time->getTimestamp());
- $startOfDay->setTime(self::BUSINESS_START, 0, 0);
- return $startOfDay;
- }
- /**
- * @param DateTime $time
- * @return integer
- */
- protected function _getWeekDay($time)
- {
- return date('N', $time->getTimestamp());
- }
- /**
- * @param DateTime $time
- * @return boolean
- */
- protected function _isInBusinessTime(DateTime $time)
- {
- if ($time->getTimestamp() <= $this->_getEndOfBusinessDay($time)->getTimestamp()
- && $time->getTimestamp() >= $this->_getStartOfBusinessDay($time)->getTimestamp()
- ) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * @param DateTime $time
- * @return boolean
- */
- protected function _isBusinessDay($time)
- {
- $weekday = date('N', $time->getTimestamp());
- if (in_array($weekday, $this->_excludedDays) === true) {
- return true;
- } else {
- return false;
- }
- }
- }
- error_reporting(-1);
- $start = time() - 12234567;
- $a = new BusinessCalc(new DateTime('@' . $start));
- echo $a->calculateSeconds(new DateTime());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement