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);
41 function __construct($period_type = 0, $date_point = null) {
45 if (!$date_point || !($date_point instanceof DateAndTime))
46 $date_point = new DateAndTime();
48 // TODO: refactoring ongoing down from here. Make code nicer, etc.
49 $weekStartDay = $user->week_start;
51 $date_begin = new DateAndTime();
52 $date_begin->setFormat($date_point->getFormat());
53 $date_end = new DateAndTime();
54 $date_end->setFormat($date_point->getFormat());
55 $t_arr = localtime($date_point->getTimestamp());
56 $t_arr[5] = $t_arr[5] + 1900;
58 if ($t_arr[6] < $weekStartDay) {
59 $startWeekBias = $weekStartDay - 7;
61 $startWeekBias = $weekStartDay;
64 switch ($period_type) {
65 case INTERVAL_THIS_DAY:
66 $date_begin->setTimestamp($date_point->getTimestamp());
67 $date_end->setTimestamp($date_point->getTimestamp());
69 case INTERVAL_THIS_WEEK:
70 $date_begin->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+$startWeekBias,$t_arr[5]));
71 $date_end->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+6+$startWeekBias,$t_arr[5]));
73 case INTERVAL_LAST_WEEK:
74 $date_begin->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]-7+$startWeekBias,$t_arr[5]));
75 $date_end->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]-1+$startWeekBias,$t_arr[5]));
77 case INTERVAL_THIS_MONTH:
78 $date_begin->setTimestamp(mktime(0,0,0,$t_arr[4]+1,1,$t_arr[5]));
79 $date_end->setTimestamp(mktime(0,0,0,$t_arr[4]+2,0,$t_arr[5]));
81 case INTERVAL_LAST_MONTH:
82 $date_begin->setTimestamp(mktime(0,0,0,$t_arr[4],1,$t_arr[5]));
83 $date_end->setTimestamp(mktime(0,0,0,$t_arr[4]+1,0,$t_arr[5]));
86 case INTERVAL_THIS_YEAR:
87 $date_begin->setTimestamp(mktime(0, 0, 0, 1, 1, $t_arr[5]));
88 $date_end->setTimestamp(mktime(0, 0, 0, 12, 31, $t_arr[5]));
91 $this->startDate = &$date_begin;
92 $this->endDate = &$date_end;
96 * Return all days by period
100 function getAllDays() {
101 $ret_array = array();
102 if ($this->startDate->before($this->endDate)) {
103 $d = $this->getBegin();
104 while ($d->before($this->getEnd())) {
105 array_push($ret_array, $d);
108 array_push($ret_array, $d);
110 array_push($ret_array, $this->startDate);
115 function setPeriod($b_date, $e_date) {
116 $this->startDate = $b_date;
117 $this->endDate = $e_date;
120 // return date object
121 function getBegin() {
122 return $this->startDate;
125 // return date object
127 return $this->endDate;
130 // return date string
131 function getBeginDate($format="") {
132 return $this->startDate->toString($format);
135 // return date string
136 function getEndDate($format="") {
137 return $this->endDate->toString($format);
140 function getArray($format="") {
142 $d = $this->getBegin();
143 while ($d->before($this->getEnd())) {
144 $result[] = $d->toString($format);