public function updateDateRange($date, $use_year = true) { $date = new LsDate($date); if ($use_year) { $date->setDay('00'); $date->setMonth('00'); } if ($date->getYear() == '0000') { return false; } //start date and end date both null, make start date the date if (!$this->end_date && !$this->start_date) { $this->start_date = (string) $date; $this->end_date = (string) $date; return true; } $end_date = new LsDate($this->end_date); $start_date = new LsDate($this->start_date); $start_date_comp = LsDate::compare($date, $start_date); $end_date_comp = LsDate::compare($date, $end_date); //if date is the same as either start date or end date, make no changes if ($start_date_comp == LsDate::COMPARE_SAME || $end_date_comp === LsDate::COMPARE_SAME) { return false; } else { if ($start_date_comp == LsDate::COMPARE_BEFORE) { $this->start_date = (string) $date; return true; } else { if ($end_date_comp == LsDate::COMPARE_AFTER) { $this->end_date = (string) $date; return true; } } } //no changes have been made return false; }
static function compare(LsDate $date1, LsDate $date2) { //if exactly one of the dates are blank if ($date1->getYear() == '0000' xor $date2->getYear() == '0000') { return self::COMPARE_UNKNOWN; } if ($date1->_year > $date2->_year) { return self::COMPARE_AFTER; } elseif ($date1->_year < $date2->_year) { return self::COMPARE_BEFORE; } else { //years are equal, gotta look closer if ($date1->_month > $date2->_month && $date2->_month) { //first month is greater than nonzero second month return self::COMPARE_AFTER; } elseif ($date1->_month < $date2->_month && $date1->_month) { //second month is greater than nonzero first month return self::COMPARE_BEFORE; } elseif ($date1->_month == $date2->_month) { //months are equal, gotta look closer if ($date1->_day > $date2->_day && $date2->_day) { //first day is greater than nonzero second day return self::COMPARE_AFTER; } elseif ($date1->_day < $date2->_day && $date1->_day) { //second day is greater than nonzero first day return self::COMPARE_BEFORE; } elseif ($date1->_day == $date2->_day) { //same dates! return self::COMPARE_SAME; } else { //days are unequal but one of them is zero return self::COMPARE_UNKNOWN; } } else { //months are unequal but one of them is zero return self::COMPARE_UNKNOWN; } } }
static function birthDatesAreCompatible(LsDate $d1, LsDate $d2, $maxYearDiff = 1) { if ($d1->isblank() || $d2->isBlank()) { return true; } elseif (abs($d1->getYear() - $d2->getYear()) <= $maxYearDiff && LsLogic::areCompatible($d1->getMonth(true), $d2->getMonth(true)) && LsLogic::areCompatible($d1->getDay(true), $d2->getDay(true))) { return true; } else { return false; } }