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 function testOverlaps() { $this->assertTrue(DateRange::create()->lazySet(Date::create('2007-03-28'), Date::create('2008-03-27'))->overlaps(DateRange::create()->lazySet(Date::create('2007-05-14'), Date::create('2008-03-29')))); $this->assertFalse(DateRange::create()->lazySet(Date::create('2007-03-28'), Date::create('2008-03-27'))->overlaps(DateRange::create()->lazySet(Date::create('2005-05-14'), Date::create('2006-03-29')))); }
/** * result is read-only, no error checking * * @return DateRange **/ public function lightCopyOnClip(DateRange $range) { $copy = DateRange::create(); if ($range->start && ($this->start && $range->start->toStamp() > $this->start->toStamp() || !$this->start)) { $copy->start = $range->start; } else { $copy->start = $this->start; } if ($range->end && ($this->end && $range->end->toStamp() < $this->end->toStamp() || !$this->end)) { $copy->end = $range->end; } else { $copy->end = $this->end; } return $copy; }
/** * @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}'"); }