aboutsummaryrefslogtreecommitdiff
path: root/src/bdrem/Event.php
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2014-01-14 17:46:51 +0100
committerChristian Weiske <cweiske@cweiske.de>2014-01-14 17:46:51 +0100
commit441c72bbf3d29be5c7f5eb0fd43ac267fe059c2f (patch)
treeadaf976068ed80a424f5acf64a754a52ce21c4e7 /src/bdrem/Event.php
downloadbdrem-441c72bbf3d29be5c7f5eb0fd43ac267fe059c2f.tar.gz
bdrem-441c72bbf3d29be5c7f5eb0fd43ac267fe059c2f.zip
first version that reads birthday reminder files
Diffstat (limited to 'src/bdrem/Event.php')
-rw-r--r--src/bdrem/Event.php117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/bdrem/Event.php b/src/bdrem/Event.php
new file mode 100644
index 0000000..65e4b9b
--- /dev/null
+++ b/src/bdrem/Event.php
@@ -0,0 +1,117 @@
+<?php
+namespace bdrem;
+
+class Event
+{
+ /**
+ * Title of the event or name of the person that has the event
+ */
+ public $title;
+
+ /**
+ * Type of the event, e.g. "birthday"
+ */
+ public $type;
+
+ /**
+ * Date of the event.
+ * ???? as year is allowed
+ *
+ * @var string YYYY-MM-DD
+ */
+ public $date;
+
+ /**
+ * Which repetition this is
+ *
+ * @var integer
+ */
+ public $age;
+
+ /**
+ * Number of days until the event (positive) or since the event (negative)
+ *
+ * @var integer
+ */
+ public $days;
+
+
+
+ public function __construct($title = null, $type = null, $date = null)
+ {
+ $this->title = $title;
+ $this->type = $type;
+ $this->date = $date;
+ }
+
+ /**
+ * Checks if the event's date is within the given date.
+ * Also calculates the age and days since the event.
+ *
+ * @return boolean True if the event's date is within the given range
+ */
+ public function isWithin($strDate, $nDaysBefore, $nDaysAfter)
+ {
+ list($rYear, $rMonth, $rDay) = explode('-', $strDate);
+ list($eYear, $eMonth, $eDay) = explode('-', $this->date);
+
+ if ($rMonth == $eMonth && $rDay == $eDay) {
+ $this->days = 0;
+ if ($eYear == '????') {
+ $this->age = null;
+ } else {
+ $this->age = $rYear - $eYear;
+ }
+ return true;
+ }
+
+ $yearOffset = 0;
+ if ($eMonth < 3 && $rMonth > 10) {
+ $yearOffset = 1;
+ } else if ($eMonth > 10 && $rMonth < 3) {
+ $yearOffset = -1;
+ }
+
+ $rD = new \DateTime($strDate);
+ $eD = new \DateTime(($rYear + $yearOffset) . '-' . $eMonth . '-' . $eDay);
+
+ $nDiff = (int) $rD->diff($eD)->format('%r%a');
+
+ $this->days = $nDiff;
+ if ($eYear == '????') {
+ $this->age = null;
+ } else {
+ $this->age = $rYear - $eYear + $yearOffset;
+ }
+
+ if ($nDiff > 0) {
+ return $nDiff <= $nDaysAfter;
+ } else {
+ return -$nDiff <= $nDaysBefore;
+ }
+
+ return false;
+ }
+
+ /**
+ * @return integer x < 0: e1 is less than e2
+ * x > 0: e1 is larger than e2
+ */
+ public static function compare(Event $e1, Event $e2)
+ {
+ list($e1Year, $e1Month, $e1Day) = explode('-', $e1->date);
+ list($e2Year, $e2Month, $e2Day) = explode('-', $e2->date);
+
+ if ($e1Month < 3 && $e2Month > 10) {
+ return 1;
+ } else if ($e1Month > 10 && $e2Month < 3) {
+ return -1;
+ } else if ($e1Month != $e2Month) {
+ return $e1Month - $e2Month;
+ } else if ($e1Day != $e2Day) {
+ return $e1Day - $e2Day;
+ }
+ return strcmp($e1->title, $e2->title);
+ }
+}
+?>