9 * @author Christian Weiske <cweiske@cweiske.de>
10 * @copyright 2014 Christian Weiske
11 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12 * @link http://cweiske.de/bdrem.htm
17 * Event model with title, type and date.
18 * Contains calculation methods
22 * @author Christian Weiske <cweiske@cweiske.de>
23 * @copyright 2014 Christian Weiske
24 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
25 * @version Release: @package_version@
26 * @link http://cweiske.de/bdrem.htm
31 * Title of the event or name of the person that has the event
37 * Type of the event, e.g. "birthday"
44 * ???? as year is allowed
46 * @var string YYYY-MM-DD
51 * Reference date against which $age and $days are calculated
54 * @var string YYYY-MM-DD
59 * "Localized" $date used for calculations against $refDate.
60 * Month and day are the same as in $date, year is near $refDate's year.
62 * @var string YYYY-MM-DD
67 * Which repetition this is
74 * Number of days until the event (positive) or since the event (negative)
85 * @param string $title Name of person the event relates to
86 * @param string $type Type of the event (e.g. "birthday")
87 * @param string $date Date of the event in format YYYY-MM-DD
89 public function __construct($title = null, $type = null, $date = null)
91 $this->title = $title;
97 * Checks if the event's date is within the given date.
98 * Also calculates the age and days since the event.
100 * @param string $strDate Date, YYYY-MM-DD
101 * @param integer $nDaysPrevious Include number of days before $strDate
102 * @param integer $nDaysNext Include number of days after $strDate
104 * @return boolean True if the event's date is within the given range
106 public function isWithin($strDate, $nDaysPrevious, $nDaysNext)
108 $this->refDate = $strDate;
109 list($rYear, $rMonth, $rDay) = explode('-', $strDate);
110 list($eYear, $eMonth, $eDay) = explode('-', $this->date);
112 if ($rMonth == $eMonth && $rDay == $eDay) {
113 $this->localDate = $strDate;
115 if ($eYear == '????') {
118 $this->age = $rYear - $eYear;
124 if ($eMonth < 3 && $rMonth > 10) {
126 } else if ($eMonth > 10 && $rMonth < 3) {
130 $this->localDate = ($rYear + $yearOffset) . '-' . $eMonth . '-' . $eDay;
131 $rD = new \DateTime($strDate);
132 $eD = new \DateTime($this->localDate);
134 $nDiff = (int) $rD->diff($eD)->format('%r%a');
136 $this->days = $nDiff;
137 if ($eYear == '????') {
140 $this->age = $rYear - $eYear + $yearOffset;
144 return $nDiff <= $nDaysNext;
146 return -$nDiff <= $nDaysPrevious;
153 * Compare two events by by their date, then by their title.
156 * @param Event $e1 Event #1
157 * @param Event $e2 Event #2
159 * @return integer x < 0: e1 is less than e2
160 * x > 0: e1 is larger than e2
162 public static function compare(Event $e1, Event $e2)
164 list($e1Year, $e1Month, $e1Day) = explode('-', $e1->date);
165 list($e2Year, $e2Month, $e2Day) = explode('-', $e2->date);
167 if ($e1Month < 3 && $e2Month > 10) {
169 } else if ($e1Month > 10 && $e2Month < 3) {
171 } else if ($e1Month != $e2Month) {
172 return $e1Month - $e2Month;
173 } else if ($e1Day != $e2Day) {
174 return $e1Day - $e2Day;
176 return strcmp($e1->title, $e2->title);