65e4b9b498b54a5d53634d14456b8adf10938c08
[bdrem.git] / src / bdrem / Event.php
1 <?php
2 namespace bdrem;
3
4 class Event
5 {
6     /**
7      * Title of the event or name of the person that has the event
8      */
9     public $title;
10
11     /**
12      * Type of the event, e.g. "birthday"
13      */
14     public $type;
15
16     /**
17      * Date of the event. 
18      * ???? as year is allowed
19      *
20      * @var string YYYY-MM-DD
21      */
22     public $date;
23
24     /**
25      * Which repetition this is
26      *
27      * @var integer
28      */
29     public $age;
30
31     /**
32      * Number of days until the event (positive) or since the event (negative)
33      *
34      * @var integer
35      */
36     public $days;
37
38
39
40     public function __construct($title = null, $type = null, $date = null)
41     {
42         $this->title = $title;
43         $this->type  = $type;
44         $this->date  = $date;
45     }
46
47     /**
48      * Checks if the event's date is within the given date.
49      * Also calculates the age and days since the event.
50      *
51      * @return boolean True if the event's date is within the given range
52      */
53     public function isWithin($strDate, $nDaysBefore, $nDaysAfter)
54     {
55         list($rYear, $rMonth, $rDay) = explode('-', $strDate);
56         list($eYear, $eMonth, $eDay) = explode('-', $this->date);
57
58         if ($rMonth == $eMonth && $rDay == $eDay) {
59             $this->days = 0;
60             if ($eYear == '????') {
61                 $this->age = null;
62             } else {
63                 $this->age = $rYear - $eYear;
64             }
65             return true;
66         }
67
68         $yearOffset = 0;
69         if ($eMonth < 3 && $rMonth > 10) {
70             $yearOffset = 1;
71         } else if ($eMonth > 10 && $rMonth < 3) {
72             $yearOffset = -1;
73         }
74
75         $rD = new \DateTime($strDate);
76         $eD = new \DateTime(($rYear + $yearOffset) . '-' . $eMonth . '-' . $eDay);
77
78         $nDiff = (int) $rD->diff($eD)->format('%r%a');
79
80         $this->days = $nDiff;
81         if ($eYear == '????') {
82             $this->age = null;
83         } else {
84             $this->age = $rYear - $eYear + $yearOffset;
85         }
86
87         if ($nDiff > 0) {
88             return $nDiff <= $nDaysAfter;
89         } else {
90             return -$nDiff <= $nDaysBefore;
91         }
92
93         return false;
94     }
95
96     /**
97      * @return integer x < 0: e1 is less than e2
98      *                 x > 0: e1 is larger than e2
99      */
100     public static function compare(Event $e1, Event $e2)
101     {
102         list($e1Year, $e1Month, $e1Day) = explode('-', $e1->date);
103         list($e2Year, $e2Month, $e2Day) = explode('-', $e2->date);
104         
105         if ($e1Month < 3 && $e2Month > 10) {
106             return 1;
107         } else if ($e1Month > 10 && $e2Month < 3) {
108             return -1;
109         } else if ($e1Month != $e2Month) {
110             return $e1Month - $e2Month;
111         } else if ($e1Day != $e2Day) {
112             return $e1Day - $e2Day;
113         }
114         return strcmp($e1->title, $e2->title);
115     }
116 }
117 ?>