1: <?php
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: class X2DateUtil {
43:
44: public static function dateBox($date) {
45: $str = '<div class="date-box" title="';
46: $str .= date('Y-m-d H:i',$date);
47:
48: $str .= '"><span class="month">';
49: $str .= date('M',$date);
50: $str .= '</span><span class="day">';
51: $str .= date('d',$date);
52: $str .= '</span></div>';
53: return $str;
54: }
55:
56: public static function actionDate($date,$priority,$complete='No') {
57: if($complete=="No"){
58: if($priority == '3')
59: $priority = ' p-3';
60: elseif($priority == '2')
61: $priority = ' p-2';
62: else
63: $priority = ' p-1';
64: }else{
65: $priority='';
66: }
67:
68: $str = '<div class="date-box'.$priority.'" title="';
69: $str .= date('Y-m-d H:i',$date);
70:
71: $str .= '"><span class="month">';
72: $str .= Yii::app()->getLocale()->getMonthName(date('n',$date),'abbreviated');
73:
74: $str .= '</span><span class="day">';
75: $str .= date('d',$date);
76: $str .= '</span></div>';
77: return $str;
78: }
79:
80: 81: 82:
83: public static function parseDateRange (
84: $range=null, $startDate=null, $endDate=null, $strict=null) {
85:
86: $dateRange = array();
87: $dateRange['strict'] = false;
88: if (isset($strict) && $strict)
89: $dateRange['strict'] = true;
90:
91: $dateRange['range'] = 'custom';
92: if (isset($range))
93: $dateRange['range'] = $range;
94:
95: switch ($dateRange['range']) {
96:
97: case 'thisDay':
98: $dateRange['start'] = strtotime('today');
99: $dateRange['end'] = time();
100: break;
101: case 'thisWeek':
102: $dateRange['start'] = strtotime('mon this week');
103: $dateRange['end'] = time();
104: break;
105: case 'thisMonth':
106: $dateRange['start'] = mktime(0, 0, 0, date('n'), 1);
107: $dateRange['end'] = time();
108: break;
109: case 'thisQuarter':
110: $retVal = self::startOfQuarter();
111: $dateRange['start'] = $retVal[0];
112: $dateRange['end'] = time();
113: break;
114: case 'thisYear':
115: $dateRange['start'] = mktime(0, 0, 0, 1, 1);
116: $dateRange['end'] = time();
117: break;
118:
119:
120: case 'lastWeek':
121: $dateRange['start'] = strtotime('mon last week');
122: $dateRange['end'] = strtotime('mon this week') - 1;
123: break;
124: case 'lastMonth':
125: $dateRange['start'] = mktime(0, 0, 0, date('n') - 1, 1);
126: $dateRange['end'] = mktime(0, 0, 0, date('n'), 1) - 1;
127: break;
128: case 'lastYear':
129: $dateRange['start'] = mktime(0, 0, 0, 1, 1, date('Y') - 1);
130: $dateRange['end'] = mktime(0, 0, 0, 1, 1, date('Y')) - 1;
131: break;
132:
133: case 'trailingDay':
134: $dateRange['start'] = strtotime('- 24 hours');
135: $dateRange['end'] = time();
136: break;
137: case 'trailingWeek':
138: $dateRange['start'] = strtotime('- 7 days');
139: $dateRange['end'] = time();
140: break;
141: case 'trailingMonth':
142: $dateRange['start'] = strtotime('- 1 month');
143: $dateRange['end'] = time();
144: break;
145: case 'trailingQuarter':
146: $dateRange['start'] = strtotime('- 3 months');
147: $dateRange['end'] = time();
148: break;
149: case 'trailingYear':
150: $dateRange['start'] = strtotime('- 1 year');
151: $dateRange['end'] = time();
152: break;
153:
154:
155: case 'all':
156: $dateRange['start'] = 0;
157: $dateRange['end'] = time();
158: if (isset($endDate)) {
159: $dateRange['end'] = Formatter::parseDate($endDate);
160: if ($dateRange['end'] == false)
161: $dateRange['end'] = time();
162: else
163: $dateRange['end'] = strtotime('23:59:59', $dateRange['end']);
164: }
165: break;
166:
167: case 'custom':
168: default:
169: $dateRange['end'] = time();
170: if (isset($endDate)) {
171: $dateRange['end'] = Formatter::parseDate($endDate);
172: if ($dateRange['end'] == false)
173: $dateRange['end'] = time();
174: else
175: $dateRange['end'] = strtotime('23:59:59', $dateRange['end']);
176: }
177:
178: $dateRange['start'] = strtotime('1 month ago', $dateRange['end']);
179: if (isset($startDate)) {
180: $dateRange['start'] = Formatter::parseDate($startDate);
181: if ($dateRange['start'] == false)
182: $dateRange['start'] = strtotime('-30 days 0:00', $dateRange['end']);
183: else
184: $dateRange['start'] = strtotime('0:00', $dateRange['start']);
185: }
186: }
187: return $dateRange;
188: }
189:
190:
191: 192: 193: 194: 195: 196: 197: 198:
199: public static function getDateRange(
200: $startKey='start',$endKey='end',$rangeKey='range', $defaultRange='custom') {
201:
202: $dateRange = array();
203: $dateRange['strict'] = false;
204: if (isset($_GET['strict']) && $_GET['strict'])
205: $dateRange['strict'] = true;
206:
207: $dateRange['range'] = $defaultRange;
208: if (isset($_GET[$rangeKey]))
209: $dateRange['range'] = $_GET[$rangeKey];
210:
211: switch ($dateRange['range']) {
212:
213: case 'thisWeek':
214: $dateRange['start'] = strtotime('mon this week');
215: $dateRange['end'] = time();
216: break;
217: case 'thisMonth':
218: $dateRange['start'] = mktime(0, 0, 0, date('n'), 1);
219: $dateRange['end'] = time();
220: break;
221: case 'lastWeek':
222: $dateRange['start'] = strtotime('mon last week');
223: $dateRange['end'] = strtotime('mon this week') - 1;
224: break;
225: case 'lastMonth':
226: $dateRange['start'] = mktime(0, 0, 0, date('n') - 1, 1);
227: $dateRange['end'] = mktime(0, 0, 0, date('n'), 1) - 1;
228: break;
229: case 'thisYear':
230: $dateRange['start'] = mktime(0, 0, 0, 1, 1);
231: $dateRange['end'] = time();
232: break;
233: case 'lastYear':
234: $dateRange['start'] = mktime(0, 0, 0, 1, 1, date('Y') - 1);
235: $dateRange['end'] = mktime(0, 0, 0, 1, 1, date('Y')) - 1;
236: break;
237: case 'all':
238: $dateRange['start'] = 0;
239: $dateRange['end'] = time();
240: if (isset($_GET[$endKey])) {
241: $dateRange['end'] = Formatter::parseDate($_GET[$endKey]);
242: if ($dateRange['end'] == false)
243: $dateRange['end'] = time();
244: else
245: $dateRange['end'] = strtotime('23:59:59', $dateRange['end']);
246: }
247: break;
248:
249: case 'custom':
250: default:
251: $dateRange['end'] = time();
252: if (isset($_GET[$endKey])) {
253: $dateRange['end'] = Formatter::parseDate($_GET[$endKey]);
254: if ($dateRange['end'] == false)
255: $dateRange['end'] = time();
256: else
257: $dateRange['end'] = strtotime('23:59:59', $dateRange['end']);
258: }
259:
260: $dateRange['start'] = strtotime('1 month ago', $dateRange['end']);
261: if (isset($_GET[$startKey])) {
262: $dateRange['start'] = Formatter::parseDate($_GET[$startKey]);
263: if ($dateRange['start'] == false)
264: $dateRange['start'] = strtotime('-30 days 0:00', $dateRange['end']);
265: else
266: $dateRange['start'] = strtotime('0:00', $dateRange['start']);
267: }
268: }
269: return $dateRange;
270: }
271:
272: public static function startOfQuarter() {
273: $current_month = date('m');
274: $current_year = date('Y');
275:
276: if($current_month>=1 && $current_month<=3) {
277: $start_date = strtotime('1-January-'.$current_year);
278: $end_date = strtotime('1-April-'.$current_year);
279: } else if($current_month>=4 && $current_month<=6) {
280: $start_date = strtotime('1-April-'.$current_year);
281: $end_date = strtotime('1-July-'.$current_year);
282: } else if($current_month>=7 && $current_month<=9) {
283: $start_date = strtotime('1-July-'.$current_year);
284: $end_date = strtotime('1-October-'.$current_year);
285: } else if($current_month>=10 && $current_month<=12) {
286: $start_date = strtotime('1-October-'.$current_year);
287: $end_date = strtotime('1-Janauary-'.($current_year+1));
288: }
289:
290: return array($start_date, $end_date);
291: }
292:
293: }
294: ?>
295: