/
Period.php
334 lines (304 loc) · 8.47 KB
/
Period.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
# MantisBT - A PHP based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* Class for actions dealing with date periods
*
* This class encapsulates all actions dealing with time intervals. It handles data
* storage, and retrieval, as well as formatting and access.
*
* @copyright Logical Outcome Ltd. 2005 - 2007
* @author Glenn Henshaw <thraxisp@logicaloutcome.ca>
* @link http://www.mantisbt.org
* @package MantisBT
* @subpackage classes
*
*/
class Period {
/**
* @var string start date
*/
var $start = '';
/**
* @var string end date
*/
var $end = '';
// ******* End vars *********************************************
/**
* Constructor
*/
function Period() {
$this->start = '';
// default to today
$this->end = '';
// default to today
}
/**
* set dates for a week
*
* @param string $p_when date string to expand to a week (Sun to Sat)
* @param int $p_weeks number of weeks
*/
function a_week( $p_when, $p_weeks = 1 ) {
list( $t_year, $t_month, $t_day ) = explode( "-", $p_when );
$t_now = getdate( mktime( 0, 0, 0, $t_month, $t_day, $t_year ) );
$this->end = strftime( "%Y-%m-%d 23:59:59", mktime( 0, 0, 0, $t_month, $t_day - $t_now['wday'] + ( $p_weeks * 7 ) - 1, $t_year ) );
$this->start = strftime( "%Y-%m-%d 00:00:00", mktime( 0, 0, 0, $t_month, $t_day - $t_now['wday'], $t_year ) );
}
/**
* set dates for this week
*
*/
function this_week() {
$this->a_week( date( 'Y-m-d' ) );
}
/**
* set dates for last week
*
* @param int $p_weeks number of weeks
*/
function last_week( $p_weeks = 1 ) {
$this->a_week( date( 'Y-m-d', strtotime( '-' . $p_weeks . ' week' ) ), $p_weeks );
}
/**
* set dates for this week to date
*
*/
function week_to_date() {
$this->this_week();
$this->end = date( 'Y-m-d' ) . ' 23:59:59';
}
/**
* set dates for a month
*
* @param string $p_when date string to expand to a month
*/
function a_month( $p_when ) {
list( $t_year, $t_month, $t_day ) = explode( "-", $p_when );
$this->end = strftime( '%Y-%m-%d 23:59:59', mktime( 0, 0, 0, $t_month + 1, 0, $t_year ) );
$this->start = strftime( '%Y-%m-%d 00:00:00', mktime( 0, 0, 0, $t_month, 1, $t_year ) );
}
/**
* set dates for this month
*
*/
function this_month() {
$this->a_month( date( 'Y-m-d' ) );
}
/**
* set dates for last month
*
*/
function last_month() {
$this->a_month( date( 'Y-m-d', strtotime( '-1 month' ) ) );
}
/**
* set dates for this month to date
*
*/
function month_to_date() {
$this->end = date( 'Y-m-d' ) . ' 23:59:59';
list( $t_year, $t_month, $t_day ) = explode( "-", $this->end );
$this->start = strftime( '%Y-%m-%d 00:00:00', mktime( 0, 0, 0, $t_month, 1, $t_year ) );
}
/**
* set dates for a quarter
*
* @param string $p_when date string to expand to a quarter
*/
function a_quarter( $p_when ) {
list( $t_year, $t_month, $t_day ) = explode( "-", $p_when );
$t_month = ( (int)(( $t_month - 1 ) / 3 ) * 3 ) + 1;
$this->end = strftime( '%Y-%m-%d 23:59:59', mktime( 0, 0, 0, $t_month + 3, 0, $t_year ) );
$this->start = strftime( '%Y-%m-%d 00:00:00', mktime( 0, 0, 0, $t_month, 1, $t_year ) );
}
/**
* set dates for this quarter
*
*/
function this_quarter() {
$this->a_quarter( date( 'Y-m-d' ) );
}
/**
* set dates for last month
*
*/
function last_quarter() {
$this->a_quarter( date( 'Y-m-d', strtotime( '-3 months' ) ) );
}
/**
* set dates for this quarter to date
*
*/
function quarter_to_date() {
$this->end = date( 'Y-m-d' ) . ' 23:59:59';
list( $t_year, $t_month, $t_day ) = explode( '-', $this->end );
$t_month = ( (int)(( $t_month - 1 ) / 3 ) * 3 ) + 1;
$this->start = strftime( '%Y-%m-%d 00:00:00', mktime( 0, 0, 0, $t_month, 1, $t_year ) );
}
/**
* set dates for a year
*
* @param string $p_when date string to expand to a year
*/
function a_year( $p_when ) {
list( $t_year, $t_month, $t_day ) = explode( '-', $p_when );
$this->end = strftime( '%Y-%m-%d 23:59:59', mktime( 0, 0, 0, 12, 31, $t_year ) );
$this->start = strftime( '%Y-%m-%d 00:00:00', mktime( 0, 0, 0, 1, 1, $t_year ) );
}
/**
* set dates for this year
*
*/
function this_year() {
$this->a_year( date( 'Y-m-d' ) );
}
/**
* set dates for current year, ending today
*
*/
function year_to_date() {
$this->end = date( 'Y-m-d' ) . ' 23:59:59';
list( $t_year, $t_month, $t_day ) = explode( "-", $this->end );
$this->start = strftime( '%Y-%m-%d 00:00:00', mktime( 0, 0, 0, 1, 1, $t_year ) );
}
/**
* set dates for last year
*
*/
function last_year() {
$this->a_year( date( 'Y-m-d', strtotime( '-1 year' ) ) );
}
/**
* get start date in unix timestamp format
*
*/
function get_start_timestamp() {
return strtotime( $this->start );
}
/**
* get end date in unix timestamp format
*
*/
function get_end_timestamp() {
return strtotime( $this->end );
}
/**
* get formatted start date
*
*/
function get_start_formatted() {
return( $this->start == '' ? '' : strftime( '%Y-%m-%d', $this->get_start_timestamp() ) );
}
/**
* get formatted end date
*
*/
function get_end_formatted() {
return( $this->end == '' ? '' : strftime( '%Y-%m-%d', $this->get_end_timestamp() ) );
}
/**
* get number of days in interval
*
*/
function get_elapsed_days() {
return( $this->get_end_timestamp() - $this->get_start_timestamp() ) / ( 24 * 60 * 60 );
}
/**
* print a period selector
*
* @param string $p_control_name
*/
function period_selector( $p_control_name ) {
$t_periods = array(
0 => plugin_lang_get( 'period_none' ),
7 => plugin_lang_get( 'period_this_week' ),
8 => plugin_lang_get( 'period_last_week' ),
9 => plugin_lang_get( 'period_two_weeks' ),
1 => plugin_lang_get( 'period_this_month' ),
2 => plugin_lang_get( 'period_last_month' ),
3 => plugin_lang_get( 'period_this_quarter' ),
4 => plugin_lang_get( 'period_last_quarter' ),
5 => plugin_lang_get( 'period_year_to_date' ),
6 => plugin_lang_get( 'period_last_year' ),
10 => plugin_lang_get( 'period_select' ),
);
$t_default = gpc_get_int( $p_control_name, 0 );
$t_formatted_start = $this->get_start_formatted();
$t_formatted_end = $this->get_end_formatted();
$t_ret = '<div id="period_menu">';
$t_ret .= get_dropdown( $t_periods, $p_control_name, $t_default, false, false );
$t_ret .= "</div>\n";
$t_ret .= "<div id=\"dates\">\n";
$t_ret .= '<label for="start_date">' . lang_get( 'from_date' ) . '</label><input type="text" id="start_date" name="start_date" size="20" value="' . $t_formatted_start . '" class="datetime" disabled="disabled" />' . "<br />\n";
$t_ret .= '<label for="end_date">' . lang_get( 'to_date' ) . '</label><input type="text" id="end_date" name="end_date" size="20" value="' . $t_formatted_end . '" class="datetime" disabled="disabled" />' . "\n";
$t_ret .= "</div>\n";
return $t_ret;
}
/**
* set date based on period selector
*
* @param string $p_control_name
* @param string $p_start_field
* @param string $p_end_field
*/
function set_period_from_selector( $p_control_name, $p_start_field = 'start_date', $p_end_field = 'end_date' ) {
$t_default = gpc_get_int( $p_control_name, 0 );
switch( $t_default ) {
case 1:
$this->month_to_date();
break;
case 2:
$this->last_month();
break;
case 3:
$this->quarter_to_date();
break;
case 4:
$this->last_quarter();
break;
case 5:
$this->year_to_date();
break;
case 6:
$this->last_year();
break;
case 7:
$this->week_to_date();
break;
case 8:
$this->last_week();
break;
case 9:
$this->last_week( 2 );
break;
case 10:
$t_today = date( 'Y-m-d' );
if( $p_start_field != '' ) {
$this->start = gpc_get_string( $p_start_field, '' ) . ' 00:00:00';
if( $this->start == '' ) {
$this->start = $t_today . ' 00:00:00';
}
}
if( $p_end_field != '' ) {
$this->end = gpc_get_string( $p_end_field, '' ) . ' 23:59:59';
if( $this->end == '' ) {
$this->end = $t_today . ' 23:59:59';
}
}
break;
default:
}
}
}