/p093_includes/Date.php
PHP | 398 lines | 321 code | 77 blank | 0 comment | 35 complexity | 04152e5f174f223a426f47a956aa9fc4 MD5 | raw file
Possible License(s): GPL-2.0
- <?php
- class Date {
- protected $_time;
- protected $_workingDaysCalendar = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
- const DAYS = 'day';
- const MINUTES = 'minute';
- const HOURS = 'hour';
- public static function create()
- {
- return new Date();
- }
- public function __construct($date = null)
- {
- if (!$date)
- {
- $this->_time = time();
- return;
- }
- if (is_string($date))
- {
- $this->_time = strtotime($date);
- }
- elseif ($date instanceof self)
- {
- $this->_time = $date->getTime();
- }
- elseif (is_numeric ((int) $date))
- {
- $this->_time = $date;
- }
- }
- public static function reverse($date)
- {
- $dateTime = preg_split('/\s/', trim($date));
- if (count($dateTime) == 0) {
- return $date;
- }
- $date = array_shift($dateTime);
- $time = '';
- if (!empty($dateTime)) {
- $time = array_shift($dateTime);
- }
- $dateTime = implode('-', array_reverse(preg_split('/\//', $date)));
- if (!empty($time)) {
- $dateTime .= " $time";
- }
- return $dateTime;
- }
- public static function format($format, $date = true) {
- if ($date === true) {
- $date = time();
- }
- if (is_int($date)) {
- return date($format, $date);
- }
- return date($format, strtotime($date));
- }
- public function getFormatted($format)
- {
- return Date::format($format, $this->getTime());
- }
- public static function now($includeTime = true)
- {
- if ($includeTime)
- {
- return date('Y-m-d H:i:s');
- }
- return date('Y-m-d');
- }
- public static function tomorrow($includeTime = true)
- {
- $format = 'Y-m-d H:i:s';
- if (!$includeTime)
- {
- $format = 'Y-m-d';
- }
- return date($format, strtotime("+1 day"));
- }
- public static function dayAfterTomorrow($includeTime = true)
- {
- $format = 'Y-m-d H:i:s';
- if (!$includeTime)
- {
- $format = 'Y-m-d';
- }
- return date($format, strtotime("+2 day"));
- }
- public function grossDaysUntil($until, $options = array())
- {
- $default = array('workingSchedule' => array('Mon' => array('8:00', '17:00'),
- 'Tue' => array('8:00', '17:00'),
- 'Wed' => array('8:00', '17:00'),
- 'Thu' => array('8:00', '17:00'),
- 'Fri' => array('8:00', '17:00'),
- 'Sat' => array('8:00', '17:00'),
- 'Sun' => array('8:00', '17:00')),
- 'hoursPerDay' => 9,
- 'lunchBreakHours' => 0);
- $options = array_merge($default, $options);
- return $this->workingDaysUntil($until, $options);
- }
- public function daysUntil($until, $exclude = array())
- {
- $secondsPerDay = 86400;
- $until = strtotime(date('Y-m-d', strtotime($until)));
- $time = strtotime(date("Y-m-d", $this->_time));
- return ($until - $time) / $secondsPerDay;
- }
- public function toSqlDate()
- {
- return date('Y-m-d H:i:s', $this->_time);
- }
- public function hoursUntil($until)
- {
- return (strtotime($until) - $this->_time) / 3600;
- }
- public function workingDaysUntil($until, $options = array())
- {
- if ($this->_time == strtotime($until))
- {
- return 0;
- }
- $default = array('workingSchedule' => array('Mon' => array('8:00', '17:00'),
- 'Tue' => array('8:00', '17:00'),
- 'Wed' => array('8:00', '17:00'),
- 'Thu' => array('8:00', '17:00'),
- 'Fri' => array('8:00', '17:00'),
- 'Sat' => array('8:00', '12:00')),
- 'hoursPerDay' => 9,
- 'lunchBreakHours' => 0);
- $options = array_merge($default, $options);
- $workingSchedule = $options['workingSchedule'];
- $currentTimestamp = strtotime(date('Y-m-d', $this->_time));
- $endTimestamp = strtotime(date('Y-m-d', strtotime($until)));
- $dayCount = 0;
- while ($currentTimestamp <= $endTimestamp)
- {
- if (!empty($workingSchedule[date('D', $currentTimestamp)]))
- {
- $currentSchedule = $workingSchedule[date('D', $currentTimestamp)];
- $d = new Date($currentSchedule[0]);
- $hoursPerDay = $d->hoursUntil($currentSchedule[1]);
- if ($hoursPerDay > 6)
- {
- $hoursPerDay -= $options['lunchBreakHours'];
- }
- $dayCount += $hoursPerDay / $options['hoursPerDay'];
- }
- $currentTimestamp = strtotime('+1 day', $currentTimestamp);
- }
- $dates = array('start' => $this->toSqlDate(), 'end' => $until);
- foreach($dates as $tag => $date)
- {
- $timestamp = strtotime($date);
- $weekDay = date('D', $timestamp);
- if (!empty($workingSchedule[$weekDay]))
- {
- $dayStartTime = strtotime($workingSchedule[$weekDay][0]);
- $dayEndTime = strtotime($workingSchedule[$weekDay][1]);
- $dateTime = strtotime(date('H:i', strtotime($date)));
- if ($dateTime < $dayStartTime)
- {
- $dateTime = $dayStartTime;
- }
- elseif ($dateTime > $dayEndTime)
- {
- $dateTime = $dayEndTime;
- }
- $diff = $dateTime - $dayStartTime;
- if ($tag == 'end')
- {
- $diff = $dayEndTime - $dateTime;
- }
- if ($diff > 0)
- {
- $dayCount -= (($diff) / (60 * 60)) / $options['hoursPerDay'];
- }
- }
- }
- return round($dayCount, 2);
- }
- public function __toString()
- {
- return $this->toSqlDate();
- }
- public function isSQLValid($date)
- {
- $tokens = preg_split('/-/', $date);
- if (count($tokens) != 3)
- {
- return false;
- }
- $year = (int) array_shift($tokens);
- $month = (int) array_shift($tokens);
- $day = (int) array_shift($tokens);
- if ($year <= 0 || $month <= 0 || $day <= 0)
- {
- return false;
- }
- return checkdate($month, $day, $year);
- }
- public function dayOfWeek($mondayFirst = true)
- {
- return (int) date('N', $this->_time);
- }
- public function isWorkingDay()
- {
- $currentDay = ucfirst(date('D', $this->_time));
- return in_array($currentDay, $this->_workingDaysCalendar);
- }
- public function nextDay()
- {
- return new Date(strtotime('+1 day', $this->_time));
- }
- public function prevDay()
- {
- return new Date(strtotime('-1 day', $this->_time));
- }
- public function nextMonth()
- {
- return new Date(strtotime('+1 month', $this->_time));
- }
- public function workingDayOfMonth()
- {
- if (!$this->isWorkingDay())
- {
- return -1;
- }
- $current = $this->_time;
- $count = 1;
- while(((int) date('j', $current)) > 1)
- {
- $current = strtotime('-1 day', $current);
- $currentDay = new Date($current);
- if ($currentDay->isWorkingDay())
- {
- $count++;
- }
- }
- return $count;
- }
- public function getWorkingDaysOfMonthCount()
- {
- $count = 0;
- $current = new Date(date('Y-m-01', $this->_time));
- $current->setWorkingDaysCalendar($this->_workingDaysCalendar);
- $end = new Date(date('Y-m-t', $this->_time));
- while ($current->getTime(false) <= $end->getTime(false))
- {
- if ($current->isWorkingDay())
- {
- $count++;
- }
- $current = new Date($current->nextDay());
- $current->setWorkingDaysCalendar($this->_workingDaysCalendar);
- }
- return $count;
- }
- public function getWorkDayOfMonthIndex()
- {
- $count = 0;
- $current = new Date(date('Y-m-01', $this->_time));
- $current->setWorkingDaysCalendar($this->_workingDaysCalendar);
- while ($current->getTime(false) <= $this->getTime(false))
- {
- if ($current->isWorkingDay())
- {
- $count++;
- }
- $current = new Date($current->nextDay());
- $current->setWorkingDaysCalendar($this->_workingDaysCalendar);
- }
- return $count;
- }
- public function getTime($includeTime = true)
- {
- if (!$includeTime)
- {
- return strtotime(date('Y-m-d', $this->_time));
- }
- return $this->_time;
- }
- public function getDate()
- {
- return new Date(date('Y-m-d', $this->_time));
- }
- public function toFormat($format)
- {
- return date($format, $this->_time);
- }
- public function add($value, $unit = self::DAYS)
- {
- if ($value > 0)
- {
- $this->_time = strtotime("+{$value} {$unit}", $this->_time);
- return $this;
- }
- $this->_time = strtotime("-{$value} {$unit}", $this->_time);
- return $this;
- }
- public function compare(self $date)
- {
- if ($this->getTime() > $date->getTime())
- {
- return 1;
- }
- elseif ($this->getTime() < $date->getTime())
- {
- return -1;
- }
- return 0;
- }
- public function setTime($time)
- {
- $this->_time = $time;
- }
- public function setWorkingDaysCalendar($workingDaysCalendar)
- {
- $this->_workingDaysCalendar = $workingDaysCalendar;
- }
- }