Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * Date utilties | |
36 | */ | |
37 | class CRM_Utils_Date { | |
38 | ||
39 | /** | |
100fef9d | 40 | * Format a date by padding it with leading '0'. |
6a488035 | 41 | * |
77855840 TO |
42 | * @param array $date |
43 | * ('Y', 'M', 'd'). | |
44 | * @param string $separator | |
45 | * The seperator to use when formatting the date. | |
f4aaa82a | 46 | * @param int|string $invalidDate what to return if the date is invalid |
6a488035 | 47 | * |
a6c01b45 CW |
48 | * @return string |
49 | * formatted string for date | |
6a488035 | 50 | */ |
00be9182 | 51 | public static function format($date, $separator = '', $invalidDate = 0) { |
6a488035 TO |
52 | if (is_numeric($date) && |
53 | ((strlen($date) == 8) || (strlen($date) == 14)) | |
54 | ) { | |
55 | return $date; | |
56 | } | |
57 | ||
58 | if (!is_array($date) || | |
59 | CRM_Utils_System::isNull($date) || | |
60 | empty($date['Y']) | |
61 | ) { | |
62 | return $invalidDate; | |
63 | } | |
64 | ||
65 | $date['Y'] = (int ) $date['Y']; | |
66 | if ($date['Y'] < 1000 || $date['Y'] > 2999) { | |
67 | return $invalidDate; | |
68 | } | |
69 | ||
70 | if (array_key_exists('m', $date)) { | |
71 | $date['M'] = $date['m']; | |
72 | } | |
73 | elseif (array_key_exists('F', $date)) { | |
74 | $date['M'] = $date['F']; | |
75 | } | |
76 | ||
a7488080 | 77 | if (!empty($date['M'])) { |
6a488035 TO |
78 | $date['M'] = (int ) $date['M']; |
79 | if ($date['M'] < 1 || $date['M'] > 12) { | |
80 | return $invalidDate; | |
81 | } | |
82 | } | |
83 | else { | |
84 | $date['M'] = 1; | |
85 | } | |
86 | ||
a7488080 | 87 | if (!empty($date['d'])) { |
6a488035 TO |
88 | $date['d'] = (int ) $date['d']; |
89 | } | |
90 | else { | |
91 | $date['d'] = 1; | |
92 | } | |
93 | ||
94 | if (!checkdate($date['M'], $date['d'], $date['Y'])) { | |
95 | return $invalidDate; | |
96 | } | |
97 | ||
98 | $date['M'] = sprintf('%02d', $date['M']); | |
99 | $date['d'] = sprintf('%02d', $date['d']); | |
100 | ||
101 | $time = ''; | |
102 | if (CRM_Utils_Array::value('H', $date) != NULL || | |
103 | CRM_Utils_Array::value('h', $date) != NULL || | |
104 | CRM_Utils_Array::value('i', $date) != NULL || | |
105 | CRM_Utils_Array::value('s', $date) != NULL | |
106 | ) { | |
107 | // we have time too.. | |
a7488080 | 108 | if (!empty($date['h'])) { |
6a488035 TO |
109 | if (CRM_Utils_Array::value('A', $date) == 'PM' or CRM_Utils_Array::value('a', $date) == 'pm') { |
110 | if ($date['h'] != 12) { | |
111 | $date['h'] = $date['h'] + 12; | |
112 | } | |
113 | } | |
114 | if ((CRM_Utils_Array::value('A', $date) == 'AM' or CRM_Utils_Array::value('a', $date) == 'am') && | |
115 | CRM_Utils_Array::value('h', $date) == 12 | |
116 | ) { | |
117 | $date['h'] = '00'; | |
118 | } | |
119 | ||
120 | $date['h'] = (int ) $date['h']; | |
121 | } | |
122 | else { | |
123 | $date['h'] = 0; | |
124 | } | |
125 | ||
126 | // in 24-hour format the hour is under the 'H' key | |
a7488080 | 127 | if (!empty($date['H'])) { |
6a488035 TO |
128 | $date['H'] = (int) $date['H']; |
129 | } | |
130 | else { | |
131 | $date['H'] = 0; | |
132 | } | |
133 | ||
a7488080 | 134 | if (!empty($date['i'])) { |
6a488035 TO |
135 | $date['i'] = (int ) $date['i']; |
136 | } | |
137 | else { | |
138 | $date['i'] = 0; | |
139 | } | |
140 | ||
141 | if ($date['h'] == 0 && $date['H'] != 0) { | |
142 | $date['h'] = $date['H']; | |
143 | } | |
144 | ||
a7488080 | 145 | if (!empty($date['s'])) { |
6a488035 TO |
146 | $date['s'] = (int ) $date['s']; |
147 | } | |
148 | else { | |
149 | $date['s'] = 0; | |
150 | } | |
151 | ||
152 | $date['h'] = sprintf('%02d', $date['h']); | |
153 | $date['i'] = sprintf('%02d', $date['i']); | |
154 | $date['s'] = sprintf('%02d', $date['s']); | |
155 | ||
156 | if ($separator) { | |
157 | $time = ' '; | |
158 | } | |
159 | $time .= $date['h'] . $separator . $date['i'] . $separator . $date['s']; | |
160 | } | |
161 | ||
162 | return $date['Y'] . $separator . $date['M'] . $separator . $date['d'] . $time; | |
163 | } | |
164 | ||
165 | /** | |
fe482240 | 166 | * Return abbreviated weekday names according to the locale. |
6a488035 | 167 | * |
1ef73b88 CW |
168 | * Array will be in localized order according to 'weekBegins' setting, |
169 | * but array keys will always match to: | |
170 | * 0 => Sun | |
171 | * 1 => Mon | |
172 | * etc. | |
173 | * | |
a6c01b45 CW |
174 | * @return array |
175 | * 0-based array with abbreviated weekday names | |
6a488035 | 176 | * |
6a488035 | 177 | */ |
1ef73b88 CW |
178 | public static function getAbbrWeekdayNames() { |
179 | static $days = array(); | |
180 | if (!$days) { | |
181 | // First day of the week | |
aaffa79f | 182 | $firstDay = Civi::settings()->get('weekBegins'); |
6a488035 TO |
183 | |
184 | // set LC_TIME and build the arrays from locale-provided names | |
185 | // June 1st, 1970 was a Monday | |
186 | CRM_Core_I18n::setLcTime(); | |
1ef73b88 CW |
187 | for ($i = $firstDay; count($days) < 7; $i = $i > 6 ? 0 : $i + 1) { |
188 | $days[$i] = strftime('%a', mktime(0, 0, 0, 6, $i, 1970)); | |
6a488035 TO |
189 | } |
190 | } | |
1ef73b88 | 191 | return $days; |
6a488035 TO |
192 | } |
193 | ||
194 | /** | |
fe482240 | 195 | * Return full weekday names according to the locale. |
6a488035 | 196 | * |
1ef73b88 CW |
197 | * Array will be in localized order according to 'weekBegins' setting, |
198 | * but array keys will always match to: | |
199 | * 0 => Sunday | |
200 | * 1 => Monday | |
201 | * etc. | |
202 | * | |
a6c01b45 CW |
203 | * @return array |
204 | * 0-based array with full weekday names | |
6a488035 | 205 | * |
6a488035 | 206 | */ |
1ef73b88 CW |
207 | public static function getFullWeekdayNames() { |
208 | static $days = array(); | |
209 | if (!$days) { | |
210 | // First day of the week | |
aaffa79f | 211 | $firstDay = Civi::settings()->get('weekBegins'); |
6a488035 TO |
212 | |
213 | // set LC_TIME and build the arrays from locale-provided names | |
214 | // June 1st, 1970 was a Monday | |
215 | CRM_Core_I18n::setLcTime(); | |
1ef73b88 CW |
216 | for ($i = $firstDay; count($days) < 7; $i = $i > 6 ? 0 : $i + 1) { |
217 | $days[$i] = strftime('%A', mktime(0, 0, 0, 6, $i, 1970)); | |
6a488035 TO |
218 | } |
219 | } | |
1ef73b88 | 220 | return $days; |
6a488035 TO |
221 | } |
222 | ||
223 | /** | |
fe482240 | 224 | * Return abbreviated month names according to the locale. |
6a488035 | 225 | * |
f4aaa82a EM |
226 | * @param bool $month |
227 | * | |
a6c01b45 CW |
228 | * @return array |
229 | * 1-based array with abbreviated month names | |
6a488035 | 230 | * |
6a488035 | 231 | */ |
00be9182 | 232 | public static function &getAbbrMonthNames($month = FALSE) { |
6a488035 TO |
233 | static $abbrMonthNames; |
234 | if (!isset($abbrMonthNames)) { | |
235 | ||
236 | // set LC_TIME and build the arrays from locale-provided names | |
237 | CRM_Core_I18n::setLcTime(); | |
238 | for ($i = 1; $i <= 12; $i++) { | |
239 | $abbrMonthNames[$i] = strftime('%b', mktime(0, 0, 0, $i, 10, 1970)); | |
240 | } | |
241 | } | |
242 | if ($month) { | |
243 | return $abbrMonthNames[$month]; | |
244 | } | |
245 | return $abbrMonthNames; | |
246 | } | |
247 | ||
248 | /** | |
fe482240 | 249 | * Return full month names according to the locale. |
6a488035 | 250 | * |
a6c01b45 CW |
251 | * @return array |
252 | * 1-based array with full month names | |
6a488035 | 253 | * |
6a488035 | 254 | */ |
00be9182 | 255 | public static function &getFullMonthNames() { |
6a488035 TO |
256 | static $fullMonthNames; |
257 | if (!isset($fullMonthNames)) { | |
258 | ||
259 | // set LC_TIME and build the arrays from locale-provided names | |
260 | CRM_Core_I18n::setLcTime(); | |
261 | for ($i = 1; $i <= 12; $i++) { | |
262 | $fullMonthNames[$i] = strftime('%B', mktime(0, 0, 0, $i, 10, 1970)); | |
263 | } | |
264 | } | |
265 | return $fullMonthNames; | |
266 | } | |
267 | ||
5bc392e6 EM |
268 | /** |
269 | * @param $string | |
270 | * | |
271 | * @return int | |
272 | */ | |
00be9182 | 273 | public static function unixTime($string) { |
6a488035 TO |
274 | if (empty($string)) { |
275 | return 0; | |
276 | } | |
277 | $parsedDate = date_parse($string); | |
278 | return mktime(CRM_Utils_Array::value('hour', $parsedDate), | |
279 | CRM_Utils_Array::value('minute', $parsedDate), | |
280 | 59, | |
281 | CRM_Utils_Array::value('month', $parsedDate), | |
282 | CRM_Utils_Array::value('day', $parsedDate), | |
283 | CRM_Utils_Array::value('year', $parsedDate) | |
284 | ); | |
285 | } | |
286 | ||
287 | /** | |
fe482240 | 288 | * Create a date and time string in a provided format. |
6a488035 TO |
289 | * |
290 | * %b - abbreviated month name ('Jan'..'Dec') | |
291 | * %B - full month name ('January'..'December') | |
292 | * %d - day of the month as a decimal number, 0-padded ('01'..'31') | |
293 | * %e - day of the month as a decimal number, blank-padded (' 1'..'31') | |
294 | * %E - day of the month as a decimal number ('1'..'31') | |
295 | * %f - English ordinal suffix for the day of the month ('st', 'nd', 'rd', 'th') | |
296 | * %H - hour in 24-hour format, 0-padded ('00'..'23') | |
297 | * %I - hour in 12-hour format, 0-padded ('01'..'12') | |
298 | * %k - hour in 24-hour format, blank-padded (' 0'..'23') | |
299 | * %l - hour in 12-hour format, blank-padded (' 1'..'12') | |
300 | * %m - month as a decimal number, 0-padded ('01'..'12') | |
301 | * %M - minute, 0-padded ('00'..'60') | |
302 | * %p - lowercase ante/post meridiem ('am', 'pm') | |
303 | * %P - uppercase ante/post meridiem ('AM', 'PM') | |
304 | * %Y - year as a decimal number including the century ('2005') | |
305 | * | |
77855840 TO |
306 | * @param string $dateString |
307 | * Date and time in 'YYYY-MM-DD hh:mm:ss' format. | |
308 | * @param string $format | |
309 | * The output format. | |
310 | * @param array $dateParts | |
311 | * An array with the desired date parts. | |
6a488035 | 312 | * |
a6c01b45 CW |
313 | * @return string |
314 | * the $format-formatted $date | |
6a488035 | 315 | */ |
00be9182 | 316 | public static function customFormat($dateString, $format = NULL, $dateParts = NULL) { |
6a488035 TO |
317 | // 1-based (January) month names arrays |
318 | $abbrMonths = self::getAbbrMonthNames(); | |
319 | $fullMonths = self::getFullMonthNames(); | |
320 | ||
321 | if (!$format) { | |
322 | $config = CRM_Core_Config::singleton(); | |
323 | ||
324 | if ($dateParts) { | |
325 | if (array_intersect(array('h', 'H'), $dateParts)) { | |
326 | $format = $config->dateformatDatetime; | |
327 | } | |
328 | elseif (array_intersect(array('d', 'j'), $dateParts)) { | |
329 | $format = $config->dateformatFull; | |
330 | } | |
331 | elseif (array_intersect(array('m', 'M'), $dateParts)) { | |
332 | $format = $config->dateformatPartial; | |
333 | } | |
334 | else { | |
335 | $format = $config->dateformatYear; | |
336 | } | |
337 | } | |
338 | else { | |
339 | if (strpos($dateString, '-')) { | |
340 | $month = (int) substr($dateString, 5, 2); | |
341 | $day = (int) substr($dateString, 8, 2); | |
342 | } | |
343 | else { | |
344 | $month = (int) substr($dateString, 4, 2); | |
345 | $day = (int) substr($dateString, 6, 2); | |
346 | } | |
347 | ||
348 | if (strlen($dateString) > 10) { | |
349 | $format = $config->dateformatDatetime; | |
350 | } | |
351 | elseif ($day > 0) { | |
352 | $format = $config->dateformatFull; | |
353 | } | |
354 | elseif ($month > 0) { | |
355 | $format = $config->dateformatPartial; | |
356 | } | |
357 | else { | |
358 | $format = $config->dateformatYear; | |
359 | } | |
360 | } | |
361 | } | |
362 | ||
35fa23f8 | 363 | if (!CRM_Utils_System::isNull($dateString)) { |
6a488035 | 364 | if (strpos($dateString, '-')) { |
353ffa53 | 365 | $year = (int) substr($dateString, 0, 4); |
6a488035 | 366 | $month = (int) substr($dateString, 5, 2); |
353ffa53 | 367 | $day = (int) substr($dateString, 8, 2); |
6a488035 TO |
368 | |
369 | $hour24 = (int) substr($dateString, 11, 2); | |
370 | $minute = (int) substr($dateString, 14, 2); | |
371 | } | |
372 | else { | |
353ffa53 | 373 | $year = (int) substr($dateString, 0, 4); |
6a488035 | 374 | $month = (int) substr($dateString, 4, 2); |
353ffa53 | 375 | $day = (int) substr($dateString, 6, 2); |
6a488035 TO |
376 | |
377 | $hour24 = (int) substr($dateString, 8, 2); | |
378 | $minute = (int) substr($dateString, 10, 2); | |
379 | } | |
380 | ||
381 | if ($day % 10 == 1 and $day != 11) { | |
382 | $suffix = 'st'; | |
383 | } | |
384 | elseif ($day % 10 == 2 and $day != 12) { | |
385 | $suffix = 'nd'; | |
386 | } | |
387 | elseif ($day % 10 == 3 and $day != 13) { | |
388 | $suffix = 'rd'; | |
389 | } | |
390 | else { | |
391 | $suffix = 'th'; | |
392 | } | |
393 | ||
394 | if ($hour24 < 12) { | |
395 | if ($hour24 == 00) { | |
396 | $hour12 = 12; | |
397 | } | |
398 | else { | |
399 | $hour12 = $hour24; | |
400 | } | |
401 | $type = 'AM'; | |
402 | } | |
403 | else { | |
404 | if ($hour24 == 12) { | |
405 | $hour12 = 12; | |
406 | } | |
407 | else { | |
408 | $hour12 = $hour24 - 12; | |
409 | } | |
410 | $type = 'PM'; | |
411 | } | |
412 | ||
413 | $date = array( | |
414 | '%b' => CRM_Utils_Array::value($month, $abbrMonths), | |
415 | '%B' => CRM_Utils_Array::value($month, $fullMonths), | |
416 | '%d' => $day > 9 ? $day : '0' . $day, | |
417 | '%e' => $day > 9 ? $day : ' ' . $day, | |
418 | '%E' => $day, | |
419 | '%f' => $suffix, | |
420 | '%H' => $hour24 > 9 ? $hour24 : '0' . $hour24, | |
421 | '%h' => $hour12 > 9 ? $hour12 : '0' . $hour12, | |
422 | '%I' => $hour12 > 9 ? $hour12 : '0' . $hour12, | |
423 | '%k' => $hour24 > 9 ? $hour24 : ' ' . $hour24, | |
424 | '%l' => $hour12 > 9 ? $hour12 : ' ' . $hour12, | |
425 | '%m' => $month > 9 ? $month : '0' . $month, | |
426 | '%M' => $minute > 9 ? $minute : '0' . $minute, | |
427 | '%i' => $minute > 9 ? $minute : '0' . $minute, | |
428 | '%p' => strtolower($type), | |
429 | '%P' => $type, | |
430 | '%A' => $type, | |
431 | '%Y' => $year, | |
432 | ); | |
433 | ||
434 | return strtr($format, $date); | |
435 | } | |
436 | else { | |
437 | return ''; | |
438 | } | |
439 | } | |
440 | ||
441 | /** | |
100fef9d | 442 | * Converts the date/datetime from MySQL format to ISO format |
6a488035 | 443 | * |
77855840 TO |
444 | * @param string $mysql |
445 | * Date/datetime in MySQL format. | |
6a488035 | 446 | * |
a6c01b45 CW |
447 | * @return string |
448 | * date/datetime in ISO format | |
6a488035 | 449 | */ |
00be9182 | 450 | public static function mysqlToIso($mysql) { |
353ffa53 TO |
451 | $year = substr($mysql, 0, 4); |
452 | $month = substr($mysql, 4, 2); | |
453 | $day = substr($mysql, 6, 2); | |
454 | $hour = substr($mysql, 8, 2); | |
6a488035 TO |
455 | $minute = substr($mysql, 10, 2); |
456 | $second = substr($mysql, 12, 2); | |
457 | ||
458 | $iso = ''; | |
459 | if ($year) { | |
460 | $iso .= "$year"; | |
461 | } | |
462 | if ($month) { | |
463 | $iso .= "-$month"; | |
464 | if ($day) { | |
465 | $iso .= "-$day"; | |
466 | } | |
467 | } | |
468 | ||
469 | if ($hour) { | |
470 | $iso .= " $hour"; | |
471 | if ($minute) { | |
472 | $iso .= ":$minute"; | |
473 | if ($second) { | |
474 | $iso .= ":$second"; | |
475 | } | |
476 | } | |
477 | } | |
478 | return $iso; | |
479 | } | |
480 | ||
481 | /** | |
100fef9d | 482 | * Converts the date/datetime from ISO format to MySQL format |
5d9f6898 EM |
483 | * Note that until CRM-14986/ 4.4.7 this was required whenever the pattern $dao->find(TRUE): $dao->save(); was |
484 | * used to update an object with a date field was used. The DAO now checks for a '-' in date field strings | |
485 | * & runs this function if the - appears - meaning it is likely redundant in the form & BAO layers | |
6a488035 | 486 | * |
77855840 TO |
487 | * @param string $iso |
488 | * Date/datetime in ISO format. | |
6a488035 | 489 | * |
a6c01b45 CW |
490 | * @return string |
491 | * date/datetime in MySQL format | |
6a488035 | 492 | */ |
00be9182 | 493 | public static function isoToMysql($iso) { |
6a488035 TO |
494 | $dropArray = array('-' => '', ':' => '', ' ' => ''); |
495 | return strtr($iso, $dropArray); | |
496 | } | |
497 | ||
498 | /** | |
100fef9d | 499 | * Converts the any given date to default date format. |
6a488035 | 500 | * |
77855840 TO |
501 | * @param array $params |
502 | * Has given date-format. | |
503 | * @param int $dateType | |
504 | * Type of date. | |
505 | * @param string $dateParam | |
506 | * Index of params. | |
f4aaa82a EM |
507 | * |
508 | * @return bool | |
6a488035 | 509 | */ |
00be9182 | 510 | public static function convertToDefaultDate(&$params, $dateType, $dateParam) { |
353ffa53 TO |
511 | $now = getdate(); |
512 | $cen = substr($now['year'], 0, 2); | |
6a488035 TO |
513 | $prevCen = $cen - 1; |
514 | ||
515 | $value = NULL; | |
a7488080 | 516 | if (!empty($params[$dateParam])) { |
6a488035 TO |
517 | // suppress hh:mm or hh:mm:ss if it exists CRM-7957 |
518 | $value = preg_replace("/(\s(([01]\d)|[2][0-3])(:([0-5]\d)){1,2})$/", "", $params[$dateParam]); | |
519 | } | |
520 | ||
521 | switch ($dateType) { | |
522 | case 1: | |
523 | if (!preg_match('/^\d\d\d\d-?(\d|\d\d)-?(\d|\d\d)$/', $value)) { | |
524 | return FALSE; | |
525 | } | |
526 | break; | |
527 | ||
528 | case 2: | |
529 | if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d$/', $value)) { | |
530 | return FALSE; | |
531 | } | |
532 | break; | |
533 | ||
534 | case 4: | |
535 | if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d$/', $value)) { | |
536 | return FALSE; | |
537 | } | |
538 | break; | |
539 | ||
540 | case 8: | |
541 | if (!preg_match('/^[A-Za-z]*.[ \t]?\d\d\,[ \t]?\d\d\d\d$/', $value)) { | |
542 | return FALSE; | |
543 | } | |
544 | break; | |
545 | ||
546 | case 16: | |
547 | if (!preg_match('/^\d\d-[A-Za-z]{3}.*-\d\d$/', $value) && !preg_match('/^\d\d[-\/]\d\d[-\/]\d\d$/', $value)) { | |
548 | return FALSE; | |
549 | } | |
550 | break; | |
551 | ||
552 | case 32: | |
553 | if (!preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d/', $value)) { | |
554 | return FALSE; | |
555 | } | |
556 | break; | |
557 | } | |
558 | ||
559 | if ($dateType == 1) { | |
560 | $formattedDate = explode("-", $value); | |
561 | if (count($formattedDate) == 3) { | |
353ffa53 | 562 | $year = (int) $formattedDate[0]; |
6a488035 | 563 | $month = (int) $formattedDate[1]; |
353ffa53 | 564 | $day = (int) $formattedDate[2]; |
6a488035 TO |
565 | } |
566 | elseif (count($formattedDate) == 1 && (strlen($value) == 8)) { | |
567 | return TRUE; | |
568 | } | |
569 | else { | |
570 | return FALSE; | |
571 | } | |
572 | } | |
573 | ||
6a488035 TO |
574 | if ($dateType == 2 || $dateType == 4) { |
575 | $formattedDate = explode("/", $value); | |
576 | if (count($formattedDate) != 3) { | |
577 | $formattedDate = explode("-", $value); | |
578 | } | |
579 | if (count($formattedDate) == 3) { | |
353ffa53 | 580 | $year = (int) $formattedDate[2]; |
6a488035 | 581 | $month = (int) $formattedDate[0]; |
353ffa53 | 582 | $day = (int) $formattedDate[1]; |
6a488035 TO |
583 | } |
584 | else { | |
585 | return FALSE; | |
586 | } | |
587 | } | |
588 | if ($dateType == 8) { | |
589 | $dateArray = explode(' ', $value); | |
590 | // ignore comma(,) | |
591 | $dateArray[1] = (int) substr($dateArray[1], 0, 2); | |
592 | ||
593 | $monthInt = 0; | |
594 | $fullMonths = self::getFullMonthNames(); | |
595 | foreach ($fullMonths as $key => $val) { | |
596 | if (strtolower($dateArray[0]) == strtolower($val)) { | |
597 | $monthInt = $key; | |
598 | break; | |
599 | } | |
600 | } | |
601 | if (!$monthInt) { | |
602 | $abbrMonths = self::getAbbrMonthNames(); | |
603 | foreach ($abbrMonths as $key => $val) { | |
604 | if (strtolower(trim($dateArray[0], ".")) == strtolower($val)) { | |
605 | $monthInt = $key; | |
606 | break; | |
607 | } | |
608 | } | |
609 | } | |
353ffa53 TO |
610 | $year = (int) $dateArray[2]; |
611 | $day = (int) $dateArray[1]; | |
6a488035 TO |
612 | $month = (int) $monthInt; |
613 | } | |
614 | if ($dateType == 16) { | |
615 | $dateArray = explode('-', $value); | |
616 | if (count($dateArray) != 3) { | |
617 | $dateArray = explode('/', $value); | |
618 | } | |
619 | ||
620 | if (count($dateArray) == 3) { | |
621 | $monthInt = 0; | |
622 | $fullMonths = self::getFullMonthNames(); | |
623 | foreach ($fullMonths as $key => $val) { | |
624 | if (strtolower($dateArray[1]) == strtolower($val)) { | |
625 | $monthInt = $key; | |
626 | break; | |
627 | } | |
628 | } | |
629 | if (!$monthInt) { | |
630 | $abbrMonths = self::getAbbrMonthNames(); | |
631 | foreach ($abbrMonths as $key => $val) { | |
632 | if (strtolower(trim($dateArray[1], ".")) == strtolower($val)) { | |
633 | $monthInt = $key; | |
634 | break; | |
635 | } | |
636 | } | |
637 | } | |
638 | if (!$monthInt) { | |
639 | $monthInt = $dateArray[1]; | |
640 | } | |
641 | ||
353ffa53 TO |
642 | $year = (int) $dateArray[2]; |
643 | $day = (int) $dateArray[0]; | |
6a488035 TO |
644 | $month = (int) $monthInt; |
645 | } | |
646 | else { | |
647 | return FALSE; | |
648 | } | |
649 | } | |
650 | if ($dateType == 32) { | |
651 | $formattedDate = explode("/", $value); | |
652 | if (count($formattedDate) == 3) { | |
353ffa53 | 653 | $year = (int) $formattedDate[2]; |
6a488035 | 654 | $month = (int) $formattedDate[1]; |
353ffa53 | 655 | $day = (int) $formattedDate[0]; |
6a488035 TO |
656 | } |
657 | else { | |
658 | return FALSE; | |
659 | } | |
660 | } | |
661 | ||
662 | $month = ($month < 10) ? "0" . "$month" : $month; | |
663 | $day = ($day < 10) ? "0" . "$day" : $day; | |
664 | ||
665 | $year = (int ) $year; | |
666 | // simple heuristic to determine what century to use | |
667 | // 00 - 20 is always 2000 - 2020 | |
668 | // 21 - 99 is always 1921 - 1999 | |
669 | if ($year < 21) { | |
670 | $year = (strlen($year) == 1) ? $cen . '0' . $year : $cen . $year; | |
671 | } | |
672 | elseif ($year < 100) { | |
673 | $year = $prevCen . $year; | |
674 | } | |
675 | ||
676 | if ($params[$dateParam]) { | |
677 | $params[$dateParam] = "$year$month$day"; | |
678 | } | |
50bfb460 | 679 | // if month is invalid return as error |
6a488035 TO |
680 | if ($month !== '00' && $month <= 12) { |
681 | return TRUE; | |
682 | } | |
683 | return FALSE; | |
684 | } | |
685 | ||
5bc392e6 EM |
686 | /** |
687 | * @param $date | |
688 | * | |
689 | * @return bool | |
690 | */ | |
00be9182 | 691 | public static function isDate(&$date) { |
6a488035 TO |
692 | if (CRM_Utils_System::isNull($date)) { |
693 | return FALSE; | |
694 | } | |
695 | return TRUE; | |
696 | } | |
697 | ||
5bc392e6 EM |
698 | /** |
699 | * @param null $timeStamp | |
700 | * | |
701 | * @return bool|string | |
702 | */ | |
00be9182 | 703 | public static function currentDBDate($timeStamp = NULL) { |
6a488035 TO |
704 | return $timeStamp ? date('YmdHis', $timeStamp) : date('YmdHis'); |
705 | } | |
706 | ||
5bc392e6 EM |
707 | /** |
708 | * @param $date | |
709 | * @param null $now | |
710 | * | |
711 | * @return bool | |
712 | */ | |
00be9182 | 713 | public static function overdue($date, $now = NULL) { |
6a488035 TO |
714 | $mysqlDate = self::isoToMysql($date); |
715 | if (!$now) { | |
716 | $now = self::currentDBDate(); | |
717 | } | |
718 | else { | |
719 | $now = self::isoToMysql($now); | |
720 | } | |
721 | ||
722 | return ($mysqlDate >= $now) ? FALSE : TRUE; | |
723 | } | |
724 | ||
725 | /** | |
fe482240 | 726 | * Get customized today. |
6a488035 TO |
727 | * |
728 | * This function is used for getting customized today. To get | |
729 | * actuall today pass 'dayParams' as null. or else pass the day, | |
730 | * month, year values as array values | |
731 | * Example: $dayParams = array( | |
353ffa53 | 732 | * 'day' => '25', 'month' => '10', |
6a488035 TO |
733 | * 'year' => '2007' ); |
734 | * | |
d5cc0fc2 | 735 | * @param array $dayParams of the day, month, year. |
77855840 | 736 | * Array of the day, month, year. |
6a488035 | 737 | * values. |
77855840 TO |
738 | * @param string $format |
739 | * Expected date format( default. | |
6a488035 TO |
740 | * format is 2007-12-21 ) |
741 | * | |
a6c01b45 | 742 | * @return string |
b44e3f84 | 743 | * Return the customized today's date (Y-m-d) |
6a488035 | 744 | */ |
00be9182 | 745 | public static function getToday($dayParams = NULL, $format = "Y-m-d") { |
6a488035 TO |
746 | if (is_null($dayParams) || empty($dayParams)) { |
747 | $today = date($format); | |
748 | } | |
749 | else { | |
750 | $today = date($format, mktime(0, 0, 0, | |
353ffa53 TO |
751 | $dayParams['month'], |
752 | $dayParams['day'], | |
753 | $dayParams['year'] | |
754 | )); | |
6a488035 TO |
755 | } |
756 | ||
757 | return $today; | |
758 | } | |
759 | ||
760 | /** | |
100fef9d | 761 | * Find whether today's date lies in |
6a488035 TO |
762 | * the given range |
763 | * | |
77855840 TO |
764 | * @param date $startDate |
765 | * Start date for the range. | |
766 | * @param date $endDate | |
767 | * End date for the range. | |
6a488035 | 768 | * |
72b3a70c CW |
769 | * @return bool |
770 | * true if today's date is in the given date range | |
6a488035 | 771 | */ |
00be9182 | 772 | public static function getRange($startDate, $endDate) { |
353ffa53 | 773 | $today = date("Y-m-d"); |
6a488035 | 774 | $mysqlStartDate = self::isoToMysql($startDate); |
353ffa53 TO |
775 | $mysqlEndDate = self::isoToMysql($endDate); |
776 | $mysqlToday = self::isoToMysql($today); | |
6a488035 TO |
777 | |
778 | if ((isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate) && ($mysqlToday <= $mysqlEndDate))) { | |
779 | return TRUE; | |
780 | } | |
781 | elseif ((isset($mysqlStartDate) && !isset($mysqlEndDate)) && (($mysqlToday >= $mysqlStartDate))) { | |
782 | return TRUE; | |
783 | } | |
784 | elseif ((!isset($mysqlStartDate) && isset($mysqlEndDate)) && (($mysqlToday <= $mysqlEndDate))) { | |
785 | return TRUE; | |
786 | } | |
787 | return FALSE; | |
788 | } | |
789 | ||
790 | /** | |
100fef9d | 791 | * Get start date and end from |
6a488035 TO |
792 | * the given relative term and unit |
793 | * | |
77855840 TO |
794 | * @param date $relative |
795 | * Eg: term.unit. | |
f4aaa82a EM |
796 | * |
797 | * @param $from | |
798 | * @param $to | |
6a488035 | 799 | * |
a6c01b45 CW |
800 | * @return array |
801 | * start date, end date | |
6a488035 | 802 | */ |
00be9182 | 803 | public static function getFromTo($relative, $from, $to) { |
6a488035 TO |
804 | if ($relative) { |
805 | list($term, $unit) = explode('.', $relative); | |
806 | $dateRange = self::relativeToAbsolute($term, $unit); | |
807 | $from = $dateRange['from']; | |
808 | //Take only Date Part, Sometime Time part is also present in 'to' | |
809 | $to = substr($dateRange['to'], 0, 8); | |
810 | } | |
811 | ||
812 | $from = self::processDate($from); | |
813 | $to = self::processDate($to, '235959'); | |
814 | ||
815 | return array($from, $to); | |
816 | } | |
817 | ||
818 | /** | |
fe482240 | 819 | * Calculate Age in Years if greater than one year else in months. |
6a488035 | 820 | * |
77855840 TO |
821 | * @param date $birthDate |
822 | * Birth Date. | |
6a488035 | 823 | * |
a6c01b45 CW |
824 | * @return int |
825 | * array $results contains years or months | |
6a488035 TO |
826 | */ |
827 | static public function calculateAge($birthDate) { | |
828 | $results = array(); | |
829 | $formatedBirthDate = CRM_Utils_Date::customFormat($birthDate, '%Y-%m-%d'); | |
830 | ||
353ffa53 TO |
831 | $bDate = explode('-', $formatedBirthDate); |
832 | $birthYear = $bDate[0]; | |
6a488035 | 833 | $birthMonth = $bDate[1]; |
353ffa53 TO |
834 | $birthDay = $bDate[2]; |
835 | $year_diff = date("Y") - $birthYear; | |
6a488035 TO |
836 | |
837 | // don't calculate age CRM-3143 | |
838 | if ($birthYear == '1902') { | |
839 | return $results; | |
840 | } | |
841 | ||
842 | switch ($year_diff) { | |
843 | case 1: | |
844 | $month = (12 - $birthMonth) + date("m"); | |
845 | if ($month < 12) { | |
846 | if (date("d") < $birthDay) { | |
847 | $month--; | |
848 | } | |
849 | $results['months'] = $month; | |
850 | } | |
851 | elseif ($month == 12 && (date("d") < $birthDay)) { | |
852 | $results['months'] = $month - 1; | |
853 | } | |
854 | else { | |
855 | $results['years'] = $year_diff; | |
856 | } | |
857 | break; | |
858 | ||
859 | case 0: | |
860 | $month = date("m") - $birthMonth; | |
861 | $results['months'] = $month; | |
862 | break; | |
863 | ||
864 | default: | |
865 | $results['years'] = $year_diff; | |
866 | if ((date("m") < $birthMonth) || (date("m") == $birthMonth) && (date("d") < $birthDay)) { | |
867 | $results['years']--; | |
868 | } | |
869 | } | |
870 | ||
871 | return $results; | |
872 | } | |
873 | ||
874 | /** | |
100fef9d | 875 | * Calculate next payment date according to provided unit & interval |
6a488035 | 876 | * |
77855840 TO |
877 | * @param string $unit |
878 | * Frequency unit like year,month, week etc. | |
6a488035 | 879 | * |
77855840 TO |
880 | * @param int $interval |
881 | * Frequency interval. | |
6a488035 | 882 | * |
77855840 TO |
883 | * @param array $date |
884 | * Start date of pledge. | |
f4aaa82a EM |
885 | * |
886 | * @param bool $dontCareTime | |
6a488035 | 887 | * |
a6c01b45 CW |
888 | * @return array |
889 | * contains new date with added interval | |
6a488035 | 890 | */ |
00be9182 | 891 | public static function intervalAdd($unit, $interval, $date, $dontCareTime = FALSE) { |
6a488035 | 892 | if (is_array($date)) { |
353ffa53 | 893 | $hour = CRM_Utils_Array::value('H', $date); |
6a488035 TO |
894 | $minute = CRM_Utils_Array::value('i', $date); |
895 | $second = CRM_Utils_Array::value('s', $date); | |
353ffa53 TO |
896 | $month = CRM_Utils_Array::value('M', $date); |
897 | $day = CRM_Utils_Array::value('d', $date); | |
898 | $year = CRM_Utils_Array::value('Y', $date); | |
6a488035 TO |
899 | } |
900 | else { | |
901 | extract(date_parse($date)); | |
902 | } | |
903 | $date = mktime($hour, $minute, $second, $month, $day, $year); | |
904 | switch ($unit) { | |
905 | case 'year': | |
906 | $date = mktime($hour, $minute, $second, $month, $day, $year + $interval); | |
907 | break; | |
908 | ||
909 | case 'month': | |
910 | $date = mktime($hour, $minute, $second, $month + $interval, $day, $year); | |
911 | break; | |
912 | ||
913 | case 'week': | |
914 | $interval = $interval * 7; | |
915 | $date = mktime($hour, $minute, $second, $month, $day + $interval, $year); | |
916 | break; | |
917 | ||
918 | case 'day': | |
919 | $date = mktime($hour, $minute, $second, $month, $day + $interval, $year); | |
920 | break; | |
921 | ||
922 | case 'second': | |
923 | $date = mktime($hour, $minute, $second + $interval, $month, $day, $year); | |
924 | break; | |
925 | } | |
926 | ||
927 | $scheduleDate = explode("-", date("n-j-Y-H-i-s", $date)); | |
928 | ||
353ffa53 | 929 | $date = array(); |
6a488035 TO |
930 | $date['M'] = $scheduleDate[0]; |
931 | $date['d'] = $scheduleDate[1]; | |
932 | $date['Y'] = $scheduleDate[2]; | |
933 | if ($dontCareTime == FALSE) { | |
934 | $date['H'] = $scheduleDate[3]; | |
935 | $date['i'] = $scheduleDate[4]; | |
936 | $date['s'] = $scheduleDate[5]; | |
937 | } | |
938 | return $date; | |
939 | } | |
940 | ||
941 | /** | |
100fef9d | 942 | * Check given format is valid for bith date. |
6a488035 TO |
943 | * and retrun supportable birth date format w/ qf mapping. |
944 | * | |
77855840 TO |
945 | * @param $format |
946 | * Given format ( eg 'M Y', 'Y M' ). | |
16b10e64 | 947 | * return array of qfMapping and date parts for date format. |
f4aaa82a EM |
948 | * |
949 | * @return array|null|string | |
6a488035 | 950 | */ |
00be9182 | 951 | public static function &checkBirthDateFormat($format = NULL) { |
6a488035 TO |
952 | $birthDateFormat = NULL; |
953 | if (!$format) { | |
954 | $birthDateFormat = self::getDateFormat('birth'); | |
955 | } | |
956 | ||
957 | $supportableFormats = array( | |
958 | 'mm/dd' => '%B %E%f', | |
959 | 'dd-mm' => '%E%f %B', | |
960 | 'yy-mm' => '%Y %B', | |
961 | 'M yy' => '%b %Y', | |
962 | 'yy' => '%Y', | |
963 | 'dd/mm/yy' => '%E%f %B %Y', | |
964 | ); | |
965 | ||
966 | if (array_key_exists($birthDateFormat, $supportableFormats)) { | |
967 | $birthDateFormat = array('qfMapping' => $supportableFormats[$birthDateFormat]); | |
968 | } | |
969 | ||
970 | return $birthDateFormat; | |
971 | } | |
972 | ||
973 | /** | |
fe482240 | 974 | * Resolves the given relative time interval into finite time limits. |
6a488035 | 975 | * |
77855840 TO |
976 | * @param array $relativeTerm |
977 | * Relative time frame like this, previous, etc. | |
978 | * @param int $unit | |
979 | * Frequency unit like year, month, week etc. | |
6a488035 | 980 | * |
a6c01b45 CW |
981 | * @return array |
982 | * start date and end date for the relative time frame | |
6a488035 | 983 | */ |
00be9182 | 984 | public static function relativeToAbsolute($relativeTerm, $unit) { |
353ffa53 TO |
985 | $now = getdate(); |
986 | $from = $to = $dateRange = array(); | |
6a488035 TO |
987 | $from['H'] = $from['i'] = $from['s'] = 0; |
988 | ||
989 | switch ($unit) { | |
990 | case 'year': | |
991 | switch ($relativeTerm) { | |
992 | case 'this': | |
993 | $from['d'] = $from['M'] = 1; | |
353ffa53 TO |
994 | $to['d'] = 31; |
995 | $to['M'] = 12; | |
996 | $to['Y'] = $from['Y'] = $now['year']; | |
6a488035 TO |
997 | break; |
998 | ||
999 | case 'previous': | |
1000 | $from['M'] = $from['d'] = 1; | |
353ffa53 TO |
1001 | $to['d'] = 31; |
1002 | $to['M'] = 12; | |
1003 | $to['Y'] = $from['Y'] = $now['year'] - 1; | |
6a488035 TO |
1004 | break; |
1005 | ||
1006 | case 'previous_before': | |
1007 | $from['M'] = $from['d'] = 1; | |
353ffa53 TO |
1008 | $to['d'] = 31; |
1009 | $to['M'] = 12; | |
1010 | $to['Y'] = $from['Y'] = $now['year'] - 2; | |
6a488035 TO |
1011 | break; |
1012 | ||
1013 | case 'previous_2': | |
1014 | $from['M'] = $from['d'] = 1; | |
353ffa53 TO |
1015 | $to['d'] = 31; |
1016 | $to['M'] = 12; | |
6a488035 | 1017 | $from['Y'] = $now['year'] - 2; |
353ffa53 | 1018 | $to['Y'] = $now['year'] - 1; |
6a488035 TO |
1019 | break; |
1020 | ||
1021 | case 'earlier': | |
1022 | $to['d'] = 31; | |
1023 | $to['M'] = 12; | |
1024 | $to['Y'] = $now['year'] - 1; | |
1025 | unset($from); | |
1026 | break; | |
1027 | ||
1028 | case 'greater': | |
1029 | $from['M'] = $from['d'] = 1; | |
1030 | $from['Y'] = $now['year']; | |
1031 | unset($to); | |
1032 | break; | |
1033 | ||
52f15bd6 | 1034 | case 'greater_previous': |
1035 | $from['d'] = 31; | |
1036 | $from['M'] = 12; | |
1037 | $from['Y'] = $now['year'] - 1; | |
1038 | unset($to); | |
1039 | break; | |
1040 | ||
6a488035 TO |
1041 | case 'ending': |
1042 | $to['d'] = $now['mday']; | |
1043 | $to['M'] = $now['mon']; | |
1044 | $to['Y'] = $now['year']; | |
1045 | $to['H'] = 23; | |
1046 | $to['i'] = $to['s'] = 59; | |
353ffa53 TO |
1047 | $from = self::intervalAdd('year', -1, $to); |
1048 | $from = self::intervalAdd('second', 1, $from); | |
6a488035 | 1049 | break; |
e902edd1 | 1050 | |
1051 | case 'current': | |
1052 | $from['M'] = $from['d'] = 1; | |
1053 | $from['Y'] = $now['year']; | |
1054 | $to['H'] = 23; | |
1055 | $to['i'] = $to['s'] = 59; | |
1056 | $to['d'] = $now['mday']; | |
1057 | $to['M'] = $now['mon']; | |
1058 | $to['Y'] = $now['year']; | |
1059 | break; | |
e2c3163d | 1060 | |
1061 | case 'ending_2': | |
1062 | $to['d'] = $now['mday']; | |
1063 | $to['M'] = $now['mon']; | |
1064 | $to['Y'] = $now['year']; | |
1065 | $to['H'] = 23; | |
1066 | $to['i'] = $to['s'] = 59; | |
353ffa53 TO |
1067 | $from = self::intervalAdd('year', -2, $to); |
1068 | $from = self::intervalAdd('second', 1, $from); | |
e2c3163d | 1069 | break; |
1070 | ||
1071 | case 'ending_3': | |
1072 | $to['d'] = $now['mday']; | |
1073 | $to['M'] = $now['mon']; | |
1074 | $to['Y'] = $now['year']; | |
1075 | $to['H'] = 23; | |
1076 | $to['i'] = $to['s'] = 59; | |
353ffa53 TO |
1077 | $from = self::intervalAdd('year', -3, $to); |
1078 | $from = self::intervalAdd('second', 1, $from); | |
e2c3163d | 1079 | break; |
c5670e9e | 1080 | |
1081 | case 'less': | |
353ffa53 TO |
1082 | $to['d'] = 31; |
1083 | $to['M'] = 12; | |
1084 | $to['Y'] = $now['year']; | |
c5670e9e | 1085 | unset($from); |
1086 | break; | |
1087 | ||
1088 | case 'next': | |
1089 | $from['M'] = $from['d'] = 1; | |
353ffa53 TO |
1090 | $to['d'] = 31; |
1091 | $to['M'] = 12; | |
1092 | $to['Y'] = $from['Y'] = $now['year'] + 1; | |
c5670e9e | 1093 | break; |
1094 | ||
1095 | case 'starting': | |
1096 | $from['d'] = $now['mday']; | |
1097 | $from['M'] = $now['mon']; | |
1098 | $from['Y'] = $now['year']; | |
1099 | $to['d'] = $now['mday'] - 1; | |
1100 | $to['M'] = $now['mon']; | |
1101 | $to['Y'] = $now['year'] + 1; | |
1102 | break; | |
6a488035 TO |
1103 | } |
1104 | break; | |
1105 | ||
1106 | case 'fiscal_year': | |
353ffa53 | 1107 | $config = CRM_Core_Config::singleton(); |
6a488035 TO |
1108 | $from['d'] = $config->fiscalYearStart['d']; |
1109 | $from['M'] = $config->fiscalYearStart['M']; | |
353ffa53 | 1110 | $fYear = self::calculateFiscalYear($from['d'], $from['M']); |
6a488035 TO |
1111 | switch ($relativeTerm) { |
1112 | case 'this': | |
353ffa53 | 1113 | $from['Y'] = $fYear; |
5eb3092f | 1114 | $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1); |
353ffa53 | 1115 | $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear)); |
6a488035 TO |
1116 | |
1117 | $to['d'] = $fiscalEnd['2']; | |
1118 | $to['M'] = $fiscalEnd['1']; | |
1119 | $to['Y'] = $fiscalEnd['0']; | |
1120 | break; | |
1121 | ||
1122 | case 'previous': | |
353ffa53 | 1123 | $from['Y'] = $fYear - 1; |
5eb3092f | 1124 | $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1); |
353ffa53 | 1125 | $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear)); |
c5670e9e | 1126 | $to['d'] = $fiscalEnd['2']; |
1127 | $to['M'] = $fiscalEnd['1']; | |
1128 | $to['Y'] = $fiscalEnd['0']; | |
1129 | break; | |
6a488035 | 1130 | |
c5670e9e | 1131 | case 'next': |
353ffa53 | 1132 | $from['Y'] = $fYear + 1; |
5eb3092f | 1133 | $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1); |
353ffa53 | 1134 | $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear)); |
6a488035 TO |
1135 | $to['d'] = $fiscalEnd['2']; |
1136 | $to['M'] = $fiscalEnd['1']; | |
1137 | $to['Y'] = $fiscalEnd['0']; | |
1138 | break; | |
1139 | } | |
1140 | break; | |
1141 | ||
1142 | case 'quarter': | |
1143 | switch ($relativeTerm) { | |
1144 | case 'this': | |
1145 | ||
353ffa53 | 1146 | $quarter = ceil($now['mon'] / 3); |
6a488035 TO |
1147 | $from['d'] = 1; |
1148 | $from['M'] = (3 * $quarter) - 2; | |
353ffa53 TO |
1149 | $to['M'] = 3 * $quarter; |
1150 | $to['Y'] = $from['Y'] = $now['year']; | |
1151 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year'])); | |
6a488035 TO |
1152 | break; |
1153 | ||
1154 | case 'previous': | |
353ffa53 TO |
1155 | $difference = 1; |
1156 | $quarter = ceil($now['mon'] / 3); | |
1157 | $quarter = $quarter - $difference; | |
6a488035 TO |
1158 | $subtractYear = 0; |
1159 | if ($quarter <= 0) { | |
1160 | $subtractYear = 1; | |
1161 | $quarter += 4; | |
1162 | } | |
1163 | $from['d'] = 1; | |
1164 | $from['M'] = (3 * $quarter) - 2; | |
353ffa53 TO |
1165 | $to['M'] = 3 * $quarter; |
1166 | $to['Y'] = $from['Y'] = $now['year'] - $subtractYear; | |
1167 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
6a488035 TO |
1168 | break; |
1169 | ||
1170 | case 'previous_before': | |
1171 | $difference = 2; | |
353ffa53 TO |
1172 | $quarter = ceil($now['mon'] / 3); |
1173 | $quarter = $quarter - $difference; | |
d75f2f47 | 1174 | $subtractYear = 0; |
6a488035 TO |
1175 | if ($quarter <= 0) { |
1176 | $subtractYear = 1; | |
1177 | $quarter += 4; | |
1178 | } | |
1179 | $from['d'] = 1; | |
1180 | $from['M'] = (3 * $quarter) - 2; | |
353ffa53 TO |
1181 | $to['M'] = 3 * $quarter; |
1182 | $to['Y'] = $from['Y'] = $now['year'] - $subtractYear; | |
1183 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
6a488035 TO |
1184 | break; |
1185 | ||
1186 | case 'previous_2': | |
353ffa53 TO |
1187 | $difference = 2; |
1188 | $quarter = ceil($now['mon'] / 3); | |
6a488035 | 1189 | $current_quarter = $quarter; |
353ffa53 TO |
1190 | $quarter = $quarter - $difference; |
1191 | $subtractYear = 0; | |
6a488035 TO |
1192 | if ($quarter <= 0) { |
1193 | $subtractYear = 1; | |
1194 | $quarter += 4; | |
1195 | } | |
1196 | $from['d'] = 1; | |
1197 | $from['M'] = (3 * $quarter) - 2; | |
1198 | switch ($current_quarter) { | |
1199 | case 1: | |
1200 | $to['M'] = (4 * $quarter); | |
1201 | break; | |
1202 | ||
1203 | case 2: | |
1204 | $to['M'] = (4 * $quarter) + 3; | |
1205 | break; | |
1206 | ||
1207 | case 3: | |
1208 | $to['M'] = (4 * $quarter) + 2; | |
1209 | break; | |
1210 | ||
1211 | case 4: | |
1212 | $to['M'] = (4 * $quarter) + 1; | |
1213 | break; | |
1214 | } | |
1215 | $to['Y'] = $from['Y'] = $now['year'] - $subtractYear; | |
1216 | if ($to['M'] > 12) { | |
1217 | $to['M'] = 3 * ($quarter - 3); | |
1218 | $to['Y'] = $now['year']; | |
1219 | } | |
1220 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
1221 | break; | |
1222 | ||
1223 | case 'earlier': | |
1224 | $quarter = ceil($now['mon'] / 3) - 1; | |
d75f2f47 | 1225 | $subtractYear = 0; |
6a488035 TO |
1226 | if ($quarter <= 0) { |
1227 | $subtractYear = 1; | |
1228 | $quarter += 4; | |
1229 | } | |
1230 | $to['M'] = 3 * $quarter; | |
1231 | $to['Y'] = $from['Y'] = $now['year'] - $subtractYear; | |
1232 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
1233 | unset($from); | |
1234 | break; | |
1235 | ||
1236 | case 'greater': | |
353ffa53 | 1237 | $quarter = ceil($now['mon'] / 3); |
6a488035 TO |
1238 | $from['d'] = 1; |
1239 | $from['M'] = (3 * $quarter) - 2; | |
1240 | $from['Y'] = $now['year']; | |
1241 | unset($to); | |
1242 | break; | |
d75f2f47 | 1243 | |
52f15bd6 | 1244 | case 'greater_previous': |
1245 | $quarter = ceil($now['mon'] / 3) - 1; | |
353ffa53 | 1246 | $subtractYear = 0; |
52f15bd6 | 1247 | if ($quarter <= 0) { |
1248 | $subtractYear = 1; | |
1249 | $quarter += 4; | |
1250 | } | |
1251 | $from['M'] = 3 * $quarter; | |
1252 | $from['Y'] = $from['Y'] = $now['year'] - $subtractYear; | |
1253 | $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y'])); | |
1254 | unset($to); | |
1255 | break; | |
6a488035 TO |
1256 | |
1257 | case 'ending': | |
1258 | $to['d'] = $now['mday']; | |
1259 | $to['M'] = $now['mon']; | |
1260 | $to['Y'] = $now['year']; | |
1261 | $to['H'] = 23; | |
1262 | $to['i'] = $to['s'] = 59; | |
2de679ba | 1263 | $from = self::intervalAdd('day', -90, $to); |
353ffa53 | 1264 | $from = self::intervalAdd('second', 1, $from); |
6a488035 | 1265 | break; |
e902edd1 | 1266 | |
1267 | case 'current': | |
353ffa53 | 1268 | $quarter = ceil($now['mon'] / 3); |
e902edd1 | 1269 | $from['d'] = 1; |
1270 | $from['M'] = (3 * $quarter) - 2; | |
1271 | $from['Y'] = $now['year']; | |
1272 | $to['d'] = $now['mday']; | |
1273 | $to['M'] = $now['mon']; | |
1274 | $to['Y'] = $now['year']; | |
1275 | $to['H'] = 23; | |
1276 | $to['i'] = $to['s'] = 59; | |
1277 | break; | |
c5670e9e | 1278 | |
1279 | case 'less': | |
353ffa53 TO |
1280 | $quarter = ceil($now['mon'] / 3); |
1281 | $to['M'] = 3 * $quarter; | |
1282 | $to['Y'] = $now['year']; | |
1283 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $now['year'])); | |
c5670e9e | 1284 | unset($from); |
1285 | break; | |
1286 | ||
1287 | case 'next': | |
1288 | $difference = -1; | |
1289 | $subtractYear = 0; | |
353ffa53 TO |
1290 | $quarter = ceil($now['mon'] / 3); |
1291 | $quarter = $quarter - $difference; | |
50bfb460 | 1292 | // CRM-14550 QA Fix |
22e263ad | 1293 | if ($quarter > 4) { |
c5670e9e | 1294 | $now['year'] = $now['year'] + 1; |
1295 | $quarter = 1; | |
1296 | } | |
1297 | if ($quarter <= 0) { | |
1298 | $subtractYear = 1; | |
1299 | $quarter += 4; | |
1300 | } | |
1301 | $from['d'] = 1; | |
1302 | $from['M'] = (3 * $quarter) - 2; | |
353ffa53 TO |
1303 | $to['M'] = 3 * $quarter; |
1304 | $to['Y'] = $from['Y'] = $now['year'] - $subtractYear; | |
1305 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
c5670e9e | 1306 | break; |
2de679ba | 1307 | |
1308 | case 'starting': | |
1309 | $from['d'] = $now['mday']; | |
1310 | $from['M'] = $now['mon']; | |
1311 | $from['Y'] = $now['year']; | |
1312 | $from['H'] = 00; | |
1313 | $from['i'] = $to['s'] = 00; | |
1314 | $to = self::intervalAdd('day', 90, $from); | |
1315 | $to = self::intervalAdd('second', -1, $to); | |
1316 | break; | |
6a488035 TO |
1317 | } |
1318 | break; | |
1319 | ||
1320 | case 'month': | |
1321 | switch ($relativeTerm) { | |
1322 | case 'this': | |
1323 | $from['d'] = 1; | |
353ffa53 | 1324 | $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year'])); |
6a488035 TO |
1325 | $from['M'] = $to['M'] = $now['mon']; |
1326 | $from['Y'] = $to['Y'] = $now['year']; | |
1327 | break; | |
1328 | ||
1329 | case 'previous': | |
1330 | $from['d'] = 1; | |
1331 | if ($now['mon'] == 1) { | |
1332 | $from['M'] = $to['M'] = 12; | |
1333 | $from['Y'] = $to['Y'] = $now['year'] - 1; | |
1334 | } | |
1335 | else { | |
1336 | $from['M'] = $to['M'] = $now['mon'] - 1; | |
1337 | $from['Y'] = $to['Y'] = $now['year']; | |
1338 | } | |
1339 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
1340 | break; | |
1341 | ||
1342 | case 'previous_before': | |
1343 | $from['d'] = 1; | |
1344 | if ($now['mon'] < 3) { | |
1345 | $from['M'] = $to['M'] = 10 + $now['mon']; | |
1346 | $from['Y'] = $to['Y'] = $now['year'] - 1; | |
1347 | } | |
1348 | else { | |
1349 | $from['M'] = $to['M'] = $now['mon'] - 2; | |
1350 | $from['Y'] = $to['Y'] = $now['year']; | |
1351 | } | |
1352 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
1353 | break; | |
1354 | ||
1355 | case 'previous_2': | |
1356 | $from['d'] = 1; | |
1357 | if ($now['mon'] < 3) { | |
1358 | $from['M'] = 10 + $now['mon']; | |
1359 | $from['Y'] = $now['year'] - 1; | |
1360 | } | |
1361 | else { | |
1362 | $from['M'] = $now['mon'] - 2; | |
1363 | $from['Y'] = $now['year']; | |
1364 | } | |
1365 | ||
1366 | if ($now['mon'] == 1) { | |
1367 | $to['M'] = 12; | |
1368 | $to['Y'] = $now['year'] - 1; | |
1369 | } | |
1370 | else { | |
1371 | $to['M'] = $now['mon'] - 1; | |
1372 | $to['Y'] = $now['year']; | |
1373 | } | |
1374 | ||
1375 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
1376 | break; | |
1377 | ||
1378 | case 'earlier': | |
50bfb460 | 1379 | // before end of past month |
6a488035 TO |
1380 | if ($now['mon'] == 1) { |
1381 | $to['M'] = 12; | |
1382 | $to['Y'] = $now['year'] - 1; | |
1383 | } | |
1384 | else { | |
1385 | $to['M'] = $now['mon'] - 1; | |
1386 | $to['Y'] = $now['year']; | |
1387 | } | |
1388 | ||
1389 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
1390 | unset($from); | |
1391 | break; | |
1392 | ||
1393 | case 'greater': | |
1394 | $from['d'] = 1; | |
1395 | $from['M'] = $now['mon'];; | |
1396 | $from['Y'] = $now['year']; | |
1397 | unset($to); | |
1398 | break; | |
1399 | ||
52f15bd6 | 1400 | case 'greater_previous': |
50bfb460 | 1401 | // from end of past month |
52f15bd6 | 1402 | if ($now['mon'] == 1) { |
1403 | $from['M'] = 12; | |
1404 | $from['Y'] = $now['year'] - 1; | |
1405 | } | |
1406 | else { | |
1407 | $from['M'] = $now['mon'] - 1; | |
1408 | $from['Y'] = $now['year']; | |
1409 | } | |
1410 | ||
1411 | $from['d'] = date('t', mktime(0, 0, 0, $from['M'], 1, $from['Y'])); | |
1412 | unset($to); | |
1413 | break; | |
1414 | ||
0db07823 | 1415 | case 'ending_2': |
1416 | $to['d'] = $now['mday']; | |
1417 | $to['M'] = $now['mon']; | |
1418 | $to['Y'] = $now['year']; | |
1419 | $to['H'] = 23; | |
1420 | $to['i'] = $to['s'] = 59; | |
1421 | $from = self::intervalAdd('day', -60, $to); | |
1422 | $from = self::intervalAdd('second', 1, $from); | |
1423 | break; | |
1424 | ||
6a488035 TO |
1425 | case 'ending': |
1426 | $to['d'] = $now['mday']; | |
1427 | $to['M'] = $now['mon']; | |
1428 | $to['Y'] = $now['year']; | |
1429 | $to['H'] = 23; | |
1430 | $to['i'] = $to['s'] = 59; | |
315eef7b | 1431 | $from = self::intervalAdd('day', -30, $to); |
353ffa53 | 1432 | $from = self::intervalAdd('second', 1, $from); |
6a488035 | 1433 | break; |
e902edd1 | 1434 | |
1435 | case 'current': | |
1436 | $from['d'] = 1; | |
1437 | $from['M'] = $now['mon'];; | |
1438 | $from['Y'] = $now['year']; | |
1439 | $to['d'] = $now['mday']; | |
1440 | $to['M'] = $now['mon']; | |
1441 | $to['Y'] = $now['year']; | |
1442 | $to['H'] = 23; | |
1443 | $to['i'] = $to['s'] = 59; | |
1444 | break; | |
c5670e9e | 1445 | |
1446 | case 'less': | |
50bfb460 | 1447 | // CRM-14550 QA Fix |
c5670e9e | 1448 | $to['Y'] = $now['year']; |
1449 | $to['M'] = $now['mon']; | |
1450 | $to['d'] = date('t', mktime(0, 0, 0, $now['mon'], 1, $now['year'])); | |
1451 | unset($from); | |
1452 | break; | |
1453 | ||
1454 | case 'next': | |
1455 | $from['d'] = 1; | |
1456 | if ($now['mon'] == 12) { | |
1457 | $from['M'] = $to['M'] = 1; | |
1458 | $from['Y'] = $to['Y'] = $now['year'] + 1; | |
1459 | } | |
1460 | else { | |
1461 | $from['M'] = $to['M'] = $now['mon'] + 1; | |
1462 | $from['Y'] = $to['Y'] = $now['year']; | |
1463 | } | |
1464 | $to['d'] = date('t', mktime(0, 0, 0, $to['M'], 1, $to['Y'])); | |
1465 | break; | |
1466 | ||
1467 | case 'starting': | |
2db7c428 | 1468 | $from['d'] = $now['mday']; |
1469 | $from['M'] = $now['mon']; | |
1470 | $from['Y'] = $now['year']; | |
1471 | $from['H'] = 00; | |
1472 | $from['i'] = $to['s'] = 00; | |
1473 | $to = self::intervalAdd('day', 30, $from); | |
0db07823 | 1474 | $to = self::intervalAdd('second', -1, $to); |
1475 | break; | |
1476 | ||
1477 | case 'starting_2': | |
1478 | $from['d'] = $now['mday']; | |
1479 | $from['M'] = $now['mon']; | |
1480 | $from['Y'] = $now['year']; | |
1481 | $from['H'] = 00; | |
1482 | $from['i'] = $to['s'] = 00; | |
1483 | $to = self::intervalAdd('day', 60, $from); | |
2db7c428 | 1484 | $to = self::intervalAdd('second', -1, $to); |
c5670e9e | 1485 | break; |
6a488035 TO |
1486 | } |
1487 | break; | |
1488 | ||
1489 | case 'week': | |
aaffa79f | 1490 | $weekFirst = Civi::settings()->get('weekBegins'); |
a3d92acb | 1491 | $thisDay = $now['wday']; |
38d131e3 | 1492 | if ($weekFirst > $thisDay) { |
a3d92acb | 1493 | $diffDay = $thisDay - $weekFirst + 7; |
1494 | } | |
1495 | else { | |
1496 | $diffDay = $thisDay - $weekFirst; | |
1497 | } | |
6a488035 TO |
1498 | switch ($relativeTerm) { |
1499 | case 'this': | |
1500 | $from['d'] = $now['mday']; | |
1501 | $from['M'] = $now['mon']; | |
1502 | $from['Y'] = $now['year']; | |
a3d92acb | 1503 | $from = self::intervalAdd('day', -1 * ($diffDay), $from); |
353ffa53 | 1504 | $to = self::intervalAdd('day', 6, $from); |
6a488035 TO |
1505 | break; |
1506 | ||
1507 | case 'previous': | |
1508 | $from['d'] = $now['mday']; | |
1509 | $from['M'] = $now['mon']; | |
1510 | $from['Y'] = $now['year']; | |
a3d92acb | 1511 | $from = self::intervalAdd('day', -1 * ($diffDay) - 7, $from); |
353ffa53 | 1512 | $to = self::intervalAdd('day', 6, $from); |
6a488035 TO |
1513 | break; |
1514 | ||
1515 | case 'previous_before': | |
1516 | $from['d'] = $now['mday']; | |
1517 | $from['M'] = $now['mon']; | |
1518 | $from['Y'] = $now['year']; | |
a3d92acb | 1519 | $from = self::intervalAdd('day', -1 * ($diffDay) - 14, $from); |
353ffa53 | 1520 | $to = self::intervalAdd('day', 6, $from); |
6a488035 TO |
1521 | break; |
1522 | ||
1523 | case 'previous_2': | |
1524 | $from['d'] = $now['mday']; | |
1525 | $from['M'] = $now['mon']; | |
1526 | $from['Y'] = $now['year']; | |
a3d92acb | 1527 | $from = self::intervalAdd('day', -1 * ($diffDay) - 14, $from); |
353ffa53 | 1528 | $to = self::intervalAdd('day', 13, $from); |
6a488035 TO |
1529 | break; |
1530 | ||
1531 | case 'earlier': | |
1532 | $to['d'] = $now['mday']; | |
1533 | $to['M'] = $now['mon']; | |
1534 | $to['Y'] = $now['year']; | |
a3d92acb | 1535 | $to = self::intervalAdd('day', -1 * ($diffDay) - 1, $to); |
6a488035 TO |
1536 | unset($from); |
1537 | break; | |
1538 | ||
1539 | case 'greater': | |
1540 | $from['d'] = $now['mday']; | |
1541 | $from['M'] = $now['mon']; | |
1542 | $from['Y'] = $now['year']; | |
a3d92acb | 1543 | $from = self::intervalAdd('day', -1 * ($diffDay), $from); |
6a488035 TO |
1544 | unset($to); |
1545 | break; | |
1546 | ||
52f15bd6 | 1547 | case 'greater_previous': |
1548 | $from['d'] = $now['mday']; | |
1549 | $from['M'] = $now['mon']; | |
1550 | $from['Y'] = $now['year']; | |
a3d92acb | 1551 | $from = self::intervalAdd('day', -1 * ($diffDay) - 1, $from); |
52f15bd6 | 1552 | unset($to); |
1553 | break; | |
1554 | ||
6a488035 TO |
1555 | case 'ending': |
1556 | $to['d'] = $now['mday']; | |
1557 | $to['M'] = $now['mon']; | |
1558 | $to['Y'] = $now['year']; | |
1559 | $to['H'] = 23; | |
1560 | $to['i'] = $to['s'] = 59; | |
353ffa53 TO |
1561 | $from = self::intervalAdd('day', -7, $to); |
1562 | $from = self::intervalAdd('second', 1, $from); | |
6a488035 | 1563 | break; |
e902edd1 | 1564 | |
1565 | case 'current': | |
1566 | $from['d'] = $now['mday']; | |
1567 | $from['M'] = $now['mon']; | |
1568 | $from['Y'] = $now['year']; | |
a3d92acb | 1569 | $from = self::intervalAdd('day', -1 * ($diffDay), $from); |
e902edd1 | 1570 | $to['d'] = $now['mday']; |
1571 | $to['M'] = $now['mon']; | |
1572 | $to['Y'] = $now['year']; | |
1573 | $to['H'] = 23; | |
1574 | $to['i'] = $to['s'] = 59; | |
1575 | break; | |
c5670e9e | 1576 | |
1577 | case 'less': | |
1578 | $to['d'] = $now['mday']; | |
1579 | $to['M'] = $now['mon']; | |
1580 | $to['Y'] = $now['year']; | |
50bfb460 | 1581 | // CRM-14550 QA Fix |
a3d92acb | 1582 | $to = self::intervalAdd('day', -1 * ($diffDay) + 6, $to); |
c5670e9e | 1583 | unset($from); |
1584 | break; | |
1585 | ||
1586 | case 'next': | |
1587 | $from['d'] = $now['mday']; | |
1588 | $from['M'] = $now['mon']; | |
1589 | $from['Y'] = $now['year']; | |
a3d92acb | 1590 | $from = self::intervalAdd('day', -1 * ($diffDay) + 7, $from); |
d5cc0fc2 | 1591 | $to = self::intervalAdd('day', 6, $from); |
c5670e9e | 1592 | break; |
1593 | ||
1594 | case 'starting': | |
1595 | $from['d'] = $now['mday']; | |
1596 | $from['M'] = $now['mon']; | |
1597 | $from['Y'] = $now['year']; | |
1598 | $from['H'] = 00; | |
1599 | $from['i'] = $to['s'] = 00; | |
d5cc0fc2 | 1600 | $to = self::intervalAdd('day', 7, $from); |
353ffa53 | 1601 | $to = self::intervalAdd('second', -1, $to); |
c5670e9e | 1602 | break; |
6a488035 TO |
1603 | } |
1604 | break; | |
1605 | ||
1606 | case 'day': | |
1607 | switch ($relativeTerm) { | |
1608 | case 'this': | |
1609 | $from['d'] = $to['d'] = $now['mday']; | |
1610 | $from['M'] = $to['M'] = $now['mon']; | |
1611 | $from['Y'] = $to['Y'] = $now['year']; | |
1612 | break; | |
1613 | ||
1614 | case 'previous': | |
1615 | $from['d'] = $now['mday']; | |
1616 | $from['M'] = $now['mon']; | |
1617 | $from['Y'] = $now['year']; | |
353ffa53 TO |
1618 | $from = self::intervalAdd('day', -1, $from); |
1619 | $to['d'] = $from['d']; | |
1620 | $to['M'] = $from['M']; | |
1621 | $to['Y'] = $from['Y']; | |
6a488035 TO |
1622 | break; |
1623 | ||
1624 | case 'previous_before': | |
1625 | $from['d'] = $now['mday']; | |
1626 | $from['M'] = $now['mon']; | |
1627 | $from['Y'] = $now['year']; | |
353ffa53 TO |
1628 | $from = self::intervalAdd('day', -2, $from); |
1629 | $to['d'] = $from['d']; | |
1630 | $to['M'] = $from['M']; | |
1631 | $to['Y'] = $from['Y']; | |
6a488035 TO |
1632 | break; |
1633 | ||
1634 | case 'previous_2': | |
1635 | $from['d'] = $to['d'] = $now['mday']; | |
1636 | $from['M'] = $to['M'] = $now['mon']; | |
1637 | $from['Y'] = $to['Y'] = $now['year']; | |
353ffa53 TO |
1638 | $from = self::intervalAdd('day', -2, $from); |
1639 | $to = self::intervalAdd('day', -1, $to); | |
6a488035 TO |
1640 | break; |
1641 | ||
1642 | case 'earlier': | |
1643 | $to['d'] = $now['mday']; | |
1644 | $to['M'] = $now['mon']; | |
1645 | $to['Y'] = $now['year']; | |
1646 | unset($from); | |
1647 | break; | |
1648 | ||
1649 | case 'greater': | |
1650 | $from['d'] = $now['mday']; | |
1651 | $from['M'] = $now['mon'];; | |
1652 | $from['Y'] = $now['year']; | |
1653 | unset($to); | |
1654 | break; | |
c5670e9e | 1655 | |
1656 | case 'starting': | |
353ffa53 TO |
1657 | $to['d'] = $now['mday']; |
1658 | $to['M'] = $now['mon']; | |
1659 | $to['Y'] = $now['year']; | |
d5cc0fc2 | 1660 | $to = self::intervalAdd('day', 1, $to); |
c5670e9e | 1661 | $from['d'] = $to['d']; |
1662 | $from['M'] = $to['M']; | |
1663 | $from['Y'] = $to['Y']; | |
1664 | break; | |
1665 | ||
6a488035 TO |
1666 | } |
1667 | break; | |
1668 | } | |
1669 | ||
1670 | foreach (array( | |
353ffa53 | 1671 | 'from', |
d5cc0fc2 | 1672 | 'to', |
353ffa53 | 1673 | ) as $item) { |
6a488035 TO |
1674 | if (!empty($$item)) { |
1675 | $dateRange[$item] = self::format($$item); | |
1676 | } | |
1677 | else { | |
1678 | $dateRange[$item] = NULL; | |
1679 | } | |
1680 | } | |
1681 | return $dateRange; | |
1682 | } | |
1683 | ||
1684 | /** | |
fe482240 | 1685 | * Calculate current fiscal year based on the fiscal month and day. |
6a488035 | 1686 | * |
77855840 TO |
1687 | * @param int $fyDate |
1688 | * Fiscal start date. | |
6a488035 | 1689 | * |
77855840 TO |
1690 | * @param int $fyMonth |
1691 | * Fiscal Start Month. | |
6a488035 | 1692 | * |
a6c01b45 CW |
1693 | * @return int |
1694 | * $fy Current Fiscl Year | |
6a488035 | 1695 | */ |
00be9182 | 1696 | public static function calculateFiscalYear($fyDate, $fyMonth) { |
6a488035 TO |
1697 | $date = date("Y-m-d"); |
1698 | $currentYear = date("Y"); | |
1699 | ||
50bfb460 | 1700 | // recalculate the date because month 4::04 make the difference |
353ffa53 TO |
1701 | $fiscalYear = explode('-', date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear))); |
1702 | $fyDate = $fiscalYear[2]; | |
1703 | $fyMonth = $fiscalYear[1]; | |
6a488035 TO |
1704 | $fyStartDate = date("Y-m-d", mktime(0, 0, 0, $fyMonth, $fyDate, $currentYear)); |
1705 | ||
1706 | if ($fyStartDate > $date) { | |
1707 | $fy = intval(intval($currentYear) - 1); | |
1708 | } | |
1709 | else { | |
1710 | $fy = intval($currentYear); | |
1711 | } | |
1712 | return $fy; | |
1713 | } | |
1714 | ||
1715 | /** | |
50bfb460 | 1716 | * Function to process date, convert to mysql format |
6a488035 | 1717 | * |
77855840 TO |
1718 | * @param string $date |
1719 | * Date string. | |
1720 | * @param string $time | |
1721 | * Time string. | |
f4aaa82a | 1722 | * @param bool|string $returnNullString 'null' needs to be returned |
6a488035 | 1723 | * so that db oject will set null in db |
77855840 TO |
1724 | * @param string $format |
1725 | * Expected return date format.( default is mysql ). | |
6a488035 | 1726 | * |
a6c01b45 CW |
1727 | * @return string |
1728 | * date format that is excepted by mysql | |
6a488035 | 1729 | */ |
00be9182 | 1730 | public static function processDate($date, $time = NULL, $returnNullString = FALSE, $format = 'YmdHis') { |
6a488035 TO |
1731 | $mysqlDate = NULL; |
1732 | ||
1733 | if ($returnNullString) { | |
1734 | $mysqlDate = 'null'; | |
1735 | } | |
1736 | ||
1737 | if (trim($date)) { | |
1738 | $mysqlDate = date($format, strtotime($date . ' ' . $time)); | |
1739 | } | |
1740 | ||
1741 | return $mysqlDate; | |
1742 | } | |
1743 | ||
1744 | /** | |
50bfb460 | 1745 | * Function to convert mysql to date plugin format. |
6a488035 | 1746 | * |
77855840 TO |
1747 | * @param string $mysqlDate |
1748 | * Date string. | |
f4aaa82a EM |
1749 | * |
1750 | * @param null $formatType | |
1751 | * @param null $format | |
1752 | * @param null $timeFormat | |
6a488035 | 1753 | * |
a6c01b45 CW |
1754 | * @return array |
1755 | * and time | |
6a488035 | 1756 | */ |
00be9182 | 1757 | public static function setDateDefaults($mysqlDate = NULL, $formatType = NULL, $format = NULL, $timeFormat = NULL) { |
6a488035 TO |
1758 | // if date is not passed assume it as today |
1759 | if (!$mysqlDate) { | |
1760 | $mysqlDate = date('Y-m-d G:i:s'); | |
1761 | } | |
1762 | ||
1763 | $config = CRM_Core_Config::singleton(); | |
1764 | if ($formatType) { | |
1765 | // get actual format | |
1766 | $params = array('name' => $formatType); | |
1767 | $values = array(); | |
1768 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values); | |
1769 | ||
1770 | if ($values['date_format']) { | |
1771 | $format = $values['date_format']; | |
1772 | } | |
1773 | ||
1774 | if (isset($values['time_format'])) { | |
1775 | $timeFormat = $values['time_format']; | |
1776 | } | |
1777 | } | |
1778 | ||
1779 | // now we set display date using js, hence we should always setdefault | |
1780 | // 'm/d/Y' format. So that submitted value is alwats mm/dd/YY format | |
1781 | // note that for date display we dynamically create text field | |
1782 | /* | |
e70a7fc0 TO |
1783 | if ( !$format ) { |
1784 | $format = $config->dateInputFormat; | |
1785 | } | |
6a488035 | 1786 | |
e70a7fc0 TO |
1787 | // get actual format |
1788 | $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats( ); | |
1789 | $dateFormat = CRM_Utils_Array::value( $format, $actualPHPFormats ); | |
1790 | */ | |
6a488035 | 1791 | |
6a488035 TO |
1792 | $dateFormat = 'm/d/Y'; |
1793 | $date = date($dateFormat, strtotime($mysqlDate)); | |
1794 | ||
1795 | if (!$timeFormat) { | |
1796 | $timeFormat = $config->timeInputFormat; | |
1797 | } | |
1798 | ||
1799 | $actualTimeFormat = "g:iA"; | |
1800 | $appendZeroLength = 7; | |
1801 | if ($timeFormat > 1) { | |
1802 | $actualTimeFormat = "G:i"; | |
1803 | $appendZeroLength = 5; | |
1804 | } | |
1805 | ||
1806 | $time = date($actualTimeFormat, strtotime($mysqlDate)); | |
1807 | ||
1808 | // need to append zero for hours < 10 | |
1809 | if (strlen($time) < $appendZeroLength) { | |
1810 | $time = '0' . $time; | |
1811 | } | |
1812 | ||
1813 | return array($date, $time); | |
1814 | } | |
1815 | ||
1816 | /** | |
fe482240 | 1817 | * Function get date format. |
6a488035 | 1818 | * |
77855840 TO |
1819 | * @param string $formatType |
1820 | * Date name e.g. birth. | |
6a488035 | 1821 | * |
a6c01b45 | 1822 | * @return string |
6a488035 | 1823 | */ |
00be9182 | 1824 | public static function getDateFormat($formatType = NULL) { |
6a488035 TO |
1825 | $format = NULL; |
1826 | if ($formatType) { | |
395d8dc6 | 1827 | $format = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_PreferencesDate', |
353ffa53 | 1828 | $formatType, 'date_format', 'name' |
6a488035 TO |
1829 | ); |
1830 | } | |
1831 | ||
1832 | if (!$format) { | |
1833 | $config = CRM_Core_Config::singleton(); | |
1834 | $format = $config->dateInputFormat; | |
1835 | } | |
1836 | return $format; | |
1837 | } | |
1838 | ||
cf142e47 DL |
1839 | /** |
1840 | * Get the time in UTC for the current time. You can optionally send an offset from the current time if needed | |
1841 | * | |
5a4f6742 CW |
1842 | * @param int $offset |
1843 | * the offset from the current time in seconds. | |
cf142e47 | 1844 | * |
72b3a70c CW |
1845 | * @return string |
1846 | * the time in UTC | |
cf142e47 | 1847 | */ |
00be9182 | 1848 | public static function getUTCTime($offset = 0) { |
6a488035 TO |
1849 | $originalTimezone = date_default_timezone_get(); |
1850 | date_default_timezone_set('UTC'); | |
1851 | $time = time() + $offset; | |
1852 | $now = date('YmdHis', $time); | |
1853 | date_default_timezone_set($originalTimezone); | |
1854 | return $now; | |
1855 | } | |
1856 | ||
1857 | ||
5bc392e6 EM |
1858 | /** |
1859 | * @param $date | |
1860 | * @param $dateType | |
1861 | * | |
1862 | * @return null|string | |
1863 | */ | |
00be9182 | 1864 | public static function formatDate($date, $dateType) { |
6a488035 TO |
1865 | $formattedDate = NULL; |
1866 | if (empty($date)) { | |
1867 | return $formattedDate; | |
1868 | } | |
1869 | ||
50bfb460 SB |
1870 | // 1. first convert date to default format. |
1871 | // 2. append time to default formatted date (might be removed during format) | |
1872 | // 3. validate date / date time. | |
1873 | // 4. If date and time then convert to default date time format. | |
6a488035 TO |
1874 | |
1875 | $dateKey = 'date'; | |
1876 | $dateParams = array($dateKey => $date); | |
1877 | ||
1878 | if (CRM_Utils_Date::convertToDefaultDate($dateParams, $dateType, $dateKey)) { | |
1879 | $dateVal = $dateParams[$dateKey]; | |
1880 | $ruleName = 'date'; | |
1881 | if ($dateType == 1) { | |
1882 | $matches = array(); | |
1883 | if (preg_match("/(\s(([01]\d)|[2][0-3]):([0-5]\d))$/", $date, $matches)) { | |
1884 | $ruleName = 'dateTime'; | |
1885 | if (strpos($date, '-') !== FALSE) { | |
1886 | $dateVal .= array_shift($matches); | |
1887 | } | |
1888 | } | |
1889 | } | |
1890 | ||
1891 | // validate date. | |
0e6e8724 | 1892 | $valid = CRM_Utils_Rule::$ruleName($dateVal); |
6a488035 TO |
1893 | |
1894 | if ($valid) { | |
50bfb460 | 1895 | // format date and time to default. |
6a488035 TO |
1896 | if ($ruleName == 'dateTime') { |
1897 | $dateVal = CRM_Utils_Date::customFormat(preg_replace("/(:|\s)?/", "", $dateVal), '%Y%m%d%H%i'); | |
50bfb460 | 1898 | // hack to add seconds |
6a488035 TO |
1899 | $dateVal .= '00'; |
1900 | } | |
1901 | $formattedDate = $dateVal; | |
1902 | } | |
1903 | } | |
1904 | ||
1905 | return $formattedDate; | |
1906 | } | |
1907 | ||
1908 | } |