2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
11 // | There are only two ways to violate the license:
13 // | 1. To redistribute this code in source form, with the copyright
14 // | notice or license removed or altered. (Distributing in compiled
15 // | forms without embedded copyright notices is permitted).
17 // | 2. To redistribute modified versions of this code in *any* form
18 // | that bears insufficient indications that the modifications are
19 // | not the work of the original author(s).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 define('INTERVAL_THIS_DAY', 1);
30 define('INTERVAL_THIS_WEEK', 2);
31 define('INTERVAL_THIS_MONTH', 3);
32 define('INTERVAL_THIS_YEAR', 4);
33 define('INTERVAL_ALL_TIME', 5);
34 define('INTERVAL_LAST_WEEK', 6);
35 define('INTERVAL_LAST_MONTH', 7);
36 define('INTERVAL_LAST_DAY', 8);
39 // Definitions for refactored code. TODO: uncomment when done.
40 define('INTERVAL_ALL_TIME', 0);
41 define('INTERVAL_CURRENT_YEAR', 10);
42 define('INTERVAL_PREVIOUS_YEAR', 14);
43 define('INTERVAL_SELECTED_YEAR', 18);
44 define('INTERVAL_CURRENT_MONTH', 20);
45 define('INTERVAL_PREVIOUS_MONTH', 24);
46 define('INTERVAL_SELECTED_MONTH', 28);
47 define('INTERVAL_CURRENT_WEEK', 30);
48 define('INTERVAL_PREVIOUS_WEEK', 34);
49 define('INTERVAL_SELECTED_WEEK', 38);
50 define('INTERVAL_CURRENT_DAY', 40);
51 define('INTERVAL_PREVIOUS_DAY', 44);
52 define('INTERVAL_SELECTED_DAY', 48);
59 function __construct($period_type = 0, $date_point = null) {
63 if (!$date_point || !($date_point instanceof DateAndTime))
64 $date_point = new DateAndTime(); // Represents current date. TODO: verify this is needed, as this is server time, not browser today.
66 // TODO: refactoring ongoing down from here. Make code nicer, etc.
67 $weekStartDay = $user->week_start;
69 $this->startDate = new DateAndTime();
70 $this->startDate->setFormat($date_point->getFormat());
71 $this->endDate = new DateAndTime();
72 $this->endDate->setFormat($date_point->getFormat());
73 $t_arr = localtime($date_point->getTimestamp());
74 $t_arr[5] = $t_arr[5] + 1900;
76 if ($t_arr[6] < $weekStartDay) {
77 $startWeekBias = $weekStartDay - 7;
79 $startWeekBias = $weekStartDay;
82 switch ($period_type) {
83 case INTERVAL_THIS_DAY:
84 $this->startDate->setTimestamp($date_point->getTimestamp());
85 $this->endDate->setTimestamp($date_point->getTimestamp());
88 case INTERVAL_LAST_DAY:
89 $this->startDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-1,$t_arr[5]));
90 $this->endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-1,$t_arr[5]));
93 case INTERVAL_THIS_WEEK:
94 $this->startDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+$startWeekBias,$t_arr[5]));
95 $this->endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+6+$startWeekBias,$t_arr[5]));
97 case INTERVAL_LAST_WEEK:
98 $this->startDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]-7+$startWeekBias,$t_arr[5]));
99 $this->endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]-1+$startWeekBias,$t_arr[5]));
101 case INTERVAL_THIS_MONTH:
102 $this->startDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,1,$t_arr[5]));
103 $this->endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+2,0,$t_arr[5]));
105 case INTERVAL_LAST_MONTH:
106 $this->startDate->setTimestamp(mktime(0,0,0,$t_arr[4],1,$t_arr[5]));
107 $this->endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,0,$t_arr[5]));
110 case INTERVAL_THIS_YEAR:
111 $this->startDate->setTimestamp(mktime(0, 0, 0, 1, 1, $t_arr[5]));
112 $this->endDate->setTimestamp(mktime(0, 0, 0, 12, 31, $t_arr[5]));
118 * Return all days by period
122 function getAllDays() {
123 $ret_array = array();
124 if ($this->startDate->before($this->endDate)) {
125 $d = $this->getBegin();
126 while ($d->before($this->getEnd())) {
127 array_push($ret_array, $d);
130 array_push($ret_array, $d);
132 array_push($ret_array, $this->startDate);
137 function setPeriod($b_date, $e_date) {
138 $this->startDate = $b_date;
139 $this->endDate = $e_date;
142 // return date object
143 function getBegin() {
144 return $this->startDate;
147 // return date object
149 return $this->endDate;
152 // return date string
153 function getBeginDate($format="") {
154 return $this->startDate->toString($format);
157 // return date string
158 function getEndDate($format="") {
159 return $this->endDate->toString($format);
162 function getArray($format="") {
164 $d = $this->getBegin();
165 while ($d->before($this->getEnd())) {
166 $result[] = $d->toString($format);