public function __construct(Date $base, $weekStart = Timestamp::WEEKDAY_MONDAY) { $firstDayOfMonth = Date::create($base->getYear() . '-' . $base->getMonth() . '-01'); $lastDayOfMonth = Date::create($base->getYear() . '-' . $base->getMonth() . '-' . date('t', $base->toStamp())); $start = $firstDayOfMonth->getFirstDayOfWeek($weekStart); $end = $lastDayOfMonth->getLastDayOfWeek($weekStart); $this->monthRange = DateRange::create()->lazySet($firstDayOfMonth, $lastDayOfMonth); $this->fullRange = DateRange::create()->lazySet($start, $end); $rawDays = $this->fullRange->split(); $this->fullLength = 0; foreach ($rawDays as $rawDay) { $day = CalendarDay::create($rawDay->toStamp()); if ($this->monthRange->contains($day)) { $day->setOutside(false); } else { $day->setOutside(true); } $this->days[$day->toDate()] = $day; $weekNumber = floor($this->fullLength / 7); if (!isset($this->weeks[$weekNumber])) { $this->weeks[$weekNumber] = CalendarWeek::create(); } $this->weeks[$weekNumber]->addDay($day); ++$this->fullLength; } ++$this->fullLength; }
public static function makeDatesListByRange(DateRange $range, IntervalUnit $unit, $hash = true) { $date = $unit->truncate($range->getStart()); if ('Date' == get_class($range->getStart())) { $date = Date::create($date->toStamp()); } $dates = []; do { if ($hash) { $dates[$date->toString()] = $date; } else { $dates[] = $date; } $date = $date->spawn('+ 1' . $unit->getName()); } while ($range->getEnd()->toStamp() >= $date->toStamp()); return $dates; }
/** * @throws WrongArgumentException * @return DateRange **/ public static function makeRange($string) { if (substr_count($string, ' - ') === 1 || substr_count($string, '-') === 1) { $delimiter = ' - '; if (substr_count($string, '-') === 1) { $delimiter = '-'; } list($start, $finish) = explode($delimiter, $string, 2); $start = self::toDate(trim($start)); $finish = self::toDate(trim($finish)); if ($start || $finish) { $range = new DateRange(); $range = DateRange::create()->lazySet($start, $finish); return $range; } elseif (trim($string) == '-') { return DateRange::create(); } } elseif ($single = self::toDate(trim($string))) { return DateRange::create()->setStart($single)->setEnd($single); } throw new WrongArgumentException("unknown string format '{$string}'"); }
public static function compare(DateRange $left, DateRange $right) { if ($left->isEmpty() && $right->isEmpty()) { return 0; } elseif ($left->isEmpty()) { return 1; } elseif ($right->isEmpty()) { return -1; } $leftStart = $left->getStartStamp(); $leftEnd = $left->getEndStamp(); $rightStart = $right->getStartStamp(); $rightEnd = $right->getEndStamp(); if (!$leftStart && !$rightStart || $leftStart && $rightStart && $leftStart == $rightStart) { if (!$leftEnd && !$rightEnd || $leftEnd && $rightEnd && $leftEnd == $rightEnd) { return 0; } elseif (!$leftEnd && $rightEnd) { return 1; } elseif ($leftEnd && !$rightEnd) { return -1; } elseif ($leftEnd < $rightEnd) { return -1; } else { return 1; } } elseif (!$leftStart && $rightStart) { return -1; } elseif ($leftStart && !$rightStart) { return 1; } elseif ($leftStart < $rightStart) { return -1; } else { return 1; } }
protected function checkRanges(DateRange $range) { return !($this->min && $this->min->toStamp() < $range->getStartStamp()) && !($this->max && $this->max->toStamp() > $range->getEndStamp()); }