PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Utils/Date.php

https://github.com/michaelmcandrew/ste
PHP | 1406 lines | 1011 code | 155 blank | 240 comment | 218 complexity | 589d4fde1355e7ea675604e83bfb6a63 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.4 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  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. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2011
  31. * $Id$
  32. *
  33. */
  34. /**
  35. * Date utilties
  36. */
  37. class CRM_Utils_Date
  38. {
  39. /**
  40. * format a date by padding it with leading '0'.
  41. *
  42. * @param array $date ('Y', 'M', 'd')
  43. * @param string $separator the seperator to use when formatting the date
  44. * @param string $invalidDate what to return if the date is invalid
  45. *
  46. * @return string - formatted string for date
  47. *
  48. * @static
  49. */
  50. static function format( $date, $separator = '', $invalidDate = 0 )
  51. {
  52. if ( is_numeric($date) &&
  53. ( ( strlen($date) == 8 ) || ( strlen($date) == 14 ) ) ) {
  54. return $date;
  55. }
  56. if ( ! is_array( $date ) ||
  57. CRM_Utils_System::isNull( $date ) ||
  58. empty( $date['Y'] ) ) {
  59. return $invalidDate;
  60. }
  61. $date['Y'] = (int ) $date['Y'];
  62. if ( $date['Y'] < 1000 || $date['Y'] > 2999 ) {
  63. return $invalidDate;
  64. }
  65. if ( array_key_exists( 'm', $date ) ) {
  66. $date['M'] = $date['m'] ;
  67. } else if ( array_key_exists('F',$date) ) {
  68. $date['M'] = $date['F'] ;
  69. }
  70. if ( CRM_Utils_Array::value( 'M', $date ) ) {
  71. $date['M'] = (int ) $date['M'];
  72. if ( $date['M'] < 1 || $date['M'] > 12 ) {
  73. return $invalidDate;
  74. }
  75. } else {
  76. $date['M'] = 1;
  77. }
  78. if ( CRM_Utils_Array::value( 'd', $date ) ) {
  79. $date['d'] = (int ) $date['d'];
  80. } else {
  81. $date['d'] = 1;
  82. }
  83. if ( ! checkdate( $date['M'], $date['d'], $date['Y'] ) ) {
  84. return $invalidDate;
  85. }
  86. $date['M'] = sprintf( '%02d', $date['M'] );
  87. $date['d'] = sprintf( '%02d', $date['d'] );
  88. $time = '';
  89. if (CRM_Utils_Array::value( 'H', $date ) != null ||
  90. CRM_Utils_Array::value( 'h', $date ) != null ||
  91. CRM_Utils_Array::value( 'i', $date ) != null ||
  92. CRM_Utils_Array::value( 's', $date ) != null) {
  93. // we have time too..
  94. if (CRM_Utils_Array::value( 'h', $date )) {
  95. if ( CRM_Utils_Array::value( 'A', $date ) == 'PM' or CRM_Utils_Array::value( 'a', $date ) == 'pm') {
  96. if ($date['h'] != 12 ) {
  97. $date['h'] = $date['h'] + 12;
  98. }
  99. }
  100. if ( (CRM_Utils_Array::value( 'A', $date ) == 'AM' or CRM_Utils_Array::value( 'a', $date ) == 'am') &&
  101. CRM_Utils_Array::value( 'h', $date ) == 12 ) {
  102. $date['h'] = '00';
  103. }
  104. $date['h'] = (int ) $date['h'];
  105. } else {
  106. $date['h'] = 0;
  107. }
  108. // in 24-hour format the hour is under the 'H' key
  109. if (CRM_Utils_Array::value('H', $date)) {
  110. $date['H'] = (int) $date['H'];
  111. } else {
  112. $date['H'] = 0;
  113. }
  114. if (CRM_Utils_Array::value( 'i', $date )) {
  115. $date['i'] = (int ) $date['i'];
  116. } else {
  117. $date['i'] = 0;
  118. }
  119. if ($date['h'] == 0 && $date['H'] != 0) {
  120. $date['h'] = $date['H'];
  121. }
  122. if (CRM_Utils_Array::value( 's', $date )) {
  123. $date['s'] = (int ) $date['s'];
  124. } else {
  125. $date['s'] = 0;
  126. }
  127. $date['h'] = sprintf( '%02d', $date['h'] );
  128. $date['i'] = sprintf( '%02d', $date['i'] );
  129. $date['s'] = sprintf( '%02d', $date['s'] );
  130. if ( $separator ) {
  131. $time = '&nbsp;';
  132. }
  133. $time .= $date['h'] . $separator . $date['i'] . $separator . $date['s'];
  134. }
  135. return $date['Y'] . $separator . $date['M'] . $separator . $date['d'] . $time;
  136. }
  137. /**
  138. * return abbreviated weekday names according to the locale
  139. *
  140. * @return array 0-based array with abbreviated weekday names
  141. *
  142. * @static
  143. */
  144. static function &getAbbrWeekdayNames()
  145. {
  146. static $abbrWeekdayNames;
  147. if (!isset($abbrWeekdayNames)) {
  148. // set LC_TIME and build the arrays from locale-provided names
  149. // June 1st, 1970 was a Monday
  150. CRM_Core_I18n::setLcTime();
  151. for ($i = 0; $i < 7; $i++) {
  152. $abbrWeekdayNames[$i] = strftime('%a', mktime(0, 0, 0, 6, $i, 1970));
  153. }
  154. }
  155. return $abbrWeekdayNames;
  156. }
  157. /**
  158. * return full weekday names according to the locale
  159. *
  160. * @return array 0-based array with full weekday names
  161. *
  162. * @static
  163. */
  164. static function &getFullWeekdayNames()
  165. {
  166. static $fullWeekdayNames;
  167. if (!isset($fullWeekdayNames)) {
  168. // set LC_TIME and build the arrays from locale-provided names
  169. // June 1st, 1970 was a Monday
  170. CRM_Core_I18n::setLcTime();
  171. for ($i = 0; $i < 7; $i++) {
  172. $fullWeekdayNames[$i] = strftime('%A', mktime(0, 0, 0, 6, $i, 1970));
  173. }
  174. }
  175. return $fullWeekdayNames;
  176. }
  177. /**
  178. * return abbreviated month names according to the locale
  179. *
  180. * @return array 1-based array with abbreviated month names
  181. *
  182. * @static
  183. */
  184. static function &getAbbrMonthNames($month=false)
  185. {
  186. static $abbrMonthNames;
  187. if (!isset($abbrMonthNames)) {
  188. // set LC_TIME and build the arrays from locale-provided names
  189. CRM_Core_I18n::setLcTime();
  190. for ($i = 1; $i <= 12; $i++) {
  191. $abbrMonthNames[$i] = strftime('%b', mktime(0, 0, 0, $i, 10, 1970 ));
  192. }
  193. }
  194. if ( $month ) {
  195. return $abbrMonthNames[$month];
  196. }
  197. return $abbrMonthNames;
  198. }
  199. /**
  200. * return full month names according to the locale
  201. *
  202. * @return array 1-based array with full month names
  203. *
  204. * @static
  205. */
  206. static function &getFullMonthNames()
  207. {
  208. static $fullMonthNames;
  209. if (!isset($fullMonthNames)) {
  210. // set LC_TIME and build the arrays from locale-provided names
  211. CRM_Core_I18n::setLcTime();
  212. for ($i = 1; $i <= 12; $i++) {
  213. $fullMonthNames[$i] = strftime('%B', mktime(0, 0, 0, $i, 10, 1970));
  214. }
  215. }
  216. return $fullMonthNames;
  217. }
  218. static function unixTime( $string )
  219. {
  220. if ( empty( $string ) ) {
  221. return 0;
  222. }
  223. $parsedDate = date_parse( $string );
  224. return mktime( CRM_Utils_Array::value( 'hour', $parsedDate ),
  225. CRM_Utils_Array::value( 'minute', $parsedDate ),
  226. 59,
  227. CRM_Utils_Array::value( 'month', $parsedDate ),
  228. CRM_Utils_Array::value( 'day', $parsedDate ),
  229. CRM_Utils_Array::value( 'year', $parsedDate ) );
  230. }
  231. /**
  232. * create a date and time string in a provided format
  233. *
  234. * %b - abbreviated month name ('Jan'..'Dec')
  235. * %B - full month name ('January'..'December')
  236. * %d - day of the month as a decimal number, 0-padded ('01'..'31')
  237. * %e - day of the month as a decimal number, blank-padded (' 1'..'31')
  238. * %E - day of the month as a decimal number ('1'..'31')
  239. * %f - English ordinal suffix for the day of the month ('st', 'nd', 'rd', 'th')
  240. * %H - hour in 24-hour format, 0-padded ('00'..'23')
  241. * %I - hour in 12-hour format, 0-padded ('01'..'12')
  242. * %k - hour in 24-hour format, blank-padded (' 0'..'23')
  243. * %l - hour in 12-hour format, blank-padded (' 1'..'12')
  244. * %m - month as a decimal number, 0-padded ('01'..'12')
  245. * %M - minute, 0-padded ('00'..'60')
  246. * %p - lowercase ante/post meridiem ('am', 'pm')
  247. * %P - uppercase ante/post meridiem ('AM', 'PM')
  248. * %Y - year as a decimal number including the century ('2005')
  249. *
  250. * @param string $date date and time in 'YYYY-MM-DD hh:mm:ss' format
  251. * @param string $format the output format
  252. * @param array $dateParts an array with the desired date parts
  253. *
  254. * @return string the $format-formatted $date
  255. *
  256. * @static
  257. */
  258. static function customFormat($dateString, $format = null, $dateParts = null)
  259. {
  260. // 1-based (January) month names arrays
  261. $abbrMonths = self::getAbbrMonthNames();
  262. $fullMonths = self::getFullMonthNames();
  263. if ( ! $format ) {
  264. $config = CRM_Core_Config::singleton();
  265. if ($dateParts) {
  266. if (array_intersect(array('h', 'H'), $dateParts)) {
  267. $format = $config->dateformatDatetime;
  268. } elseif (array_intersect(array('d', 'j'), $dateParts)) {
  269. $format = $config->dateformatFull;
  270. } elseif (array_intersect(array('m', 'M'), $dateParts)) {
  271. $format = $config->dateformatPartial;
  272. } else {
  273. $format = $config->dateformatYear;
  274. }
  275. } else {
  276. if ( strpos($dateString, '-') ) {
  277. $month = (int) substr($dateString, 5, 2);
  278. $day = (int) substr($dateString, 8, 2);
  279. } else {
  280. $month = (int) substr($dateString, 4, 2);
  281. $day = (int) substr($dateString, 6, 2);
  282. }
  283. if (strlen($dateString) > 10) {
  284. $format = $config->dateformatDatetime;
  285. } elseif ($day > 0) {
  286. $format = $config->dateformatFull;
  287. } elseif ($month > 0) {
  288. $format = $config->dateformatPartial;
  289. } else {
  290. $format = $config->dateformatYear;
  291. }
  292. }
  293. }
  294. if ($dateString) {
  295. if ( strpos($dateString, '-') ) {
  296. $year = (int) substr($dateString, 0, 4);
  297. $month = (int) substr($dateString, 5, 2);
  298. $day = (int) substr($dateString, 8, 2);
  299. $hour24 = (int) substr($dateString, 11, 2);
  300. $minute = (int) substr($dateString, 14, 2);
  301. } else {
  302. $year = (int) substr($dateString, 0, 4);
  303. $month = (int) substr($dateString, 4, 2);
  304. $day = (int) substr($dateString, 6, 2);
  305. $hour24 = (int) substr($dateString, 8, 2);
  306. $minute = (int) substr($dateString, 10, 2);
  307. }
  308. if ($day % 10 == 1 and $day != 11) $suffix = 'st';
  309. elseif ($day % 10 == 2 and $day != 12) $suffix = 'nd';
  310. elseif ($day % 10 == 3 and $day != 13) $suffix = 'rd';
  311. else $suffix = 'th';
  312. if ($hour24 < 12) {
  313. if ($hour24 == 00) $hour12 = 12;
  314. else $hour12 = $hour24;
  315. $type = 'AM';
  316. } else {
  317. if ($hour24 == 12) $hour12 = 12;
  318. else $hour12 = $hour24 - 12;
  319. $type = 'PM';
  320. }
  321. $date = array(
  322. '%b' => CRM_Utils_Array::value( $month, $abbrMonths ),
  323. '%B' => CRM_Utils_Array::value( $month, $fullMonths ),
  324. '%d' => $day > 9 ? $day : '0' . $day,
  325. '%e' => $day > 9 ? $day : ' ' . $day,
  326. '%E' => $day,
  327. '%f' => $suffix,
  328. '%H' => $hour24 > 9 ? $hour24 : '0' . $hour24,
  329. '%h' => $hour12 > 9 ? $hour12 : '0' . $hour12,
  330. '%I' => $hour12 > 9 ? $hour12 : '0' . $hour12,
  331. '%k' => $hour24 > 9 ? $hour24 : ' ' . $hour24,
  332. '%l' => $hour12 > 9 ? $hour12 : ' ' . $hour12,
  333. '%m' => $month > 9 ? $month : '0' . $month,
  334. '%M' => $minute > 9 ? $minute : '0' . $minute,
  335. '%i' => $minute > 9 ? $minute : '0' . $minute,
  336. '%p' => strtolower($type),
  337. '%P' => $type,
  338. '%A' => $type,
  339. '%Y' => $year
  340. );
  341. return strtr($format, $date);
  342. } else {
  343. return '';
  344. }
  345. }
  346. /**
  347. * converts the date/datetime from MySQL format to ISO format
  348. *
  349. * @param string $mysql date/datetime in MySQL format
  350. * @return string date/datetime in ISO format
  351. * @static
  352. */
  353. static function mysqlToIso($mysql)
  354. {
  355. $year = substr($mysql, 0, 4);
  356. $month = substr($mysql, 4, 2);
  357. $day = substr($mysql, 6, 2);
  358. $hour = substr($mysql, 8, 2);
  359. $minute = substr($mysql, 10, 2);
  360. $second = substr($mysql, 12, 2);
  361. $iso = '';
  362. if ($year) $iso .= "$year";
  363. if ($month) {
  364. $iso .= "-$month";
  365. if ( $day ) {
  366. $iso .= "-$day";
  367. }
  368. }
  369. if ($hour) {
  370. $iso .= " $hour";
  371. if ($minute) {
  372. $iso .= ":$minute";
  373. if ($second) {
  374. $iso .= ":$second";
  375. }
  376. }
  377. }
  378. return $iso;
  379. }
  380. /**
  381. * converts the date/datetime from ISO format to MySQL format
  382. *
  383. * @param string $iso date/datetime in ISO format
  384. * @return string date/datetime in MySQL format
  385. * @static
  386. */
  387. static function isoToMysql($iso)
  388. {
  389. $dropArray = array('-' => '', ':' => '', ' ' => '');
  390. return strtr($iso, $dropArray);
  391. }
  392. /**
  393. * converts the any given date to default date format.
  394. *
  395. * @param array $params has given date-format
  396. * @param int $dateType type of date
  397. * @param string $dateParam index of params
  398. * @static
  399. */
  400. function convertToDefaultDate( &$params, $dateType, $dateParam )
  401. {
  402. $now = getDate();
  403. $cen = substr($now['year'], 0, 2);
  404. $prevCen = $cen - 1;
  405. $value = null;
  406. if ( CRM_Utils_Array::value( $dateParam, $params ) ) {
  407. // suppress hh:mm or hh:mm:ss if it exists CRM-7957
  408. $value = preg_replace( "/(\s(([01]\d)|[2][0-3])(:([0-5]\d)){1,2})$/", "", $params[$dateParam] );
  409. }
  410. switch( $dateType ) {
  411. case 1 :
  412. if ( ! preg_match('/^\d\d\d\d-?(\d|\d\d)-?(\d|\d\d)$/', $value) ) {
  413. return false;
  414. }
  415. break;
  416. case 2 :
  417. if ( ! preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d$/', $value) ) {
  418. return false;
  419. }
  420. break;
  421. case 4 :
  422. if ( ! preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d$/', $value ) ) {
  423. return false;
  424. }
  425. break;
  426. case 8 :
  427. if ( ! preg_match('/^[A-Za-z]*.[ \t]?\d\d\,[ \t]?\d\d\d\d$/', $value ) ) {
  428. return false;
  429. }
  430. break;
  431. case 16 :
  432. if ( ! preg_match('/^\d\d-[A-Za-z]{3}.*-\d\d$/', $value ) && ! preg_match('/^\d\d[-\/]\d\d[-\/]\d\d$/', $value ) ) {
  433. return false;
  434. }
  435. break;
  436. case 32 :
  437. if ( ! preg_match('/^(\d|\d\d)[-\/](\d|\d\d)[-\/]\d\d\d\d/', $value) ) {
  438. return false;
  439. }
  440. break;
  441. }
  442. if ( $dateType == 1 ) {
  443. $formattedDate = explode( "-", $value );
  444. if ( count($formattedDate) == 3 ) {
  445. $year = (int) $formattedDate[0];
  446. $month = (int) $formattedDate[1];
  447. $day = (int) $formattedDate[2];
  448. } else if ( count($formattedDate) == 1 && ( strlen($value) == 8 ) ){
  449. return true;
  450. } else {
  451. return false;
  452. }
  453. }
  454. if ( $dateType == 2 || $dateType == 4) {
  455. $formattedDate = explode( "/", $value );
  456. if ( count($formattedDate) != 3 ) {
  457. $formattedDate = explode( "-" , $value );
  458. }
  459. if ( count($formattedDate) == 3 ) {
  460. $year = (int) $formattedDate[2];
  461. $month = (int) $formattedDate[0];
  462. $day = (int) $formattedDate[1];
  463. } else {
  464. return false;
  465. }
  466. }
  467. if ( $dateType == 8 ) {
  468. $dateArray = explode(' ',$value);
  469. $dateArray[1] = (int) substr($dateArray[1], 0, 2); //ignore comma(,)
  470. $monthInt = 0;
  471. $fullMonths = self::getFullMonthNames();
  472. foreach ($fullMonths as $key => $val) {
  473. if (strtolower($dateArray[0]) == strtolower($val)) {
  474. $monthInt = $key;
  475. break;
  476. }
  477. }
  478. if (!$monthInt) {
  479. $abbrMonths = self::getAbbrMonthNames();
  480. foreach ($abbrMonths as $key => $val) {
  481. if (strtolower(trim($dateArray[0], "." )) == strtolower($val)) {
  482. $monthInt = $key;
  483. break;
  484. }
  485. }
  486. }
  487. $year = (int) $dateArray[2];
  488. $day = (int) $dateArray[1];
  489. $month = (int) $monthInt;
  490. }
  491. if ( $dateType == 16 ) {
  492. $dateArray = explode('-',$value);
  493. if ( count ( $dateArray ) != 3 ) {
  494. $dateArray = explode('/', $value);
  495. }
  496. if ( count ( $dateArray ) == 3 ) {
  497. $monthInt = 0;
  498. $fullMonths = self::getFullMonthNames();
  499. foreach ( $fullMonths as $key => $val ) {
  500. if ( strtolower( $dateArray[1] ) == strtolower( $val )) {
  501. $monthInt = $key;
  502. break;
  503. }
  504. }
  505. if ( !$monthInt ) {
  506. $abbrMonths = self::getAbbrMonthNames();
  507. foreach ( $abbrMonths as $key => $val ) {
  508. if ( strtolower(trim($dateArray[1], "." )) == strtolower( $val )) {
  509. $monthInt = $key;
  510. break;
  511. }
  512. }
  513. }
  514. if ( !$monthInt ) {
  515. $monthInt = $dateArray[1];
  516. }
  517. $year = (int) $dateArray[2];
  518. $day = (int) $dateArray[0];
  519. $month = (int) $monthInt;
  520. } else {
  521. return false;
  522. }
  523. }
  524. if ( $dateType == 32 ) {
  525. $formattedDate = explode( "/", $value );
  526. if ( count($formattedDate) == 3 ) {
  527. $year = (int) $formattedDate[2];
  528. $month = (int) $formattedDate[1];
  529. $day = (int) $formattedDate[0];
  530. } else {
  531. return false;
  532. }
  533. }
  534. $month = ($month < 10)? "0" . "$month" : $month;
  535. $day = ($day < 10) ? "0" . "$day" : $day;
  536. $year = (int ) $year;
  537. // simple heuristic to determine what century to use
  538. // 00 - 20 is always 2000 - 2020
  539. // 21 - 99 is always 1921 - 1999
  540. if ( $year < 21 ) {
  541. $year = ( strlen( $year ) == 1 ) ? $cen . '0' . $year : $cen . $year;
  542. } else if ( $year < 100 ) {
  543. $year = $prevCen . $year;
  544. }
  545. if ($params[$dateParam]) {
  546. $params[$dateParam] = "$year$month$day";
  547. }
  548. //if month is invalid return as error
  549. if ( $month !== '00' && $month <= 12 ) {
  550. return true;
  551. }
  552. return false;
  553. }
  554. static function isDate( &$date )
  555. {
  556. if ( CRM_Utils_System::isNull( $date ) ) {
  557. return false;
  558. }
  559. return true;
  560. }
  561. static function currentDBDate( $timeStamp = null ) {
  562. return $timeStamp ?
  563. date( 'YmdHis', $timeStamp ) : date( 'YmdHis' );
  564. }
  565. static function overdue( $date, $now = null )
  566. {
  567. $mysqlDate = self::isoToMysql( $date );
  568. if ( ! $now ) {
  569. $now = self::currentDBDate( );
  570. } else {
  571. $now = self::isoToMysql( $now );
  572. }
  573. return ( $mysqlDate >= $now ) ? false : true;
  574. }
  575. /**
  576. * Function to get customized today
  577. *
  578. * This function is used for getting customized today. To get
  579. * actuall today pass 'dayParams' as null. or else pass the day,
  580. * month, year values as array values
  581. * Example: $dayParams = array( 'day' => '25', 'month' => '10',
  582. * 'year' => '2007' );
  583. *
  584. * @param Array $dayParams Array of the day, month, year
  585. * values.
  586. * @param string $format expected date format( default
  587. * format is 2007-12-21 )
  588. *
  589. * @return string Return the customized todays date (Y-m-d)
  590. * @static
  591. */
  592. static function getToday( $dayParams = null, $format = "Y-m-d" )
  593. {
  594. if ( is_null( $dayParams ) || empty( $dayParams ) ) {
  595. $today = date( $format );
  596. } else {
  597. $today = date( $format, mktime( 0, 0, 0,
  598. $dayParams['month'],
  599. $dayParams['day'],
  600. $dayParams['year'] ) );
  601. }
  602. return $today;
  603. }
  604. /**
  605. * Function to find whether today's date lies in
  606. * the given range
  607. *
  608. * @param date $startDate start date for the range
  609. * @param date $endDate end date for the range
  610. *
  611. * @return true todays date is in the given date range
  612. * @static
  613. */
  614. static function getRange( $startDate, $endDate )
  615. {
  616. $today = date( "Y-m-d" );
  617. $mysqlStartDate = self::isoToMysql( $startDate );
  618. $mysqlEndDate = self::isoToMysql( $endDate );
  619. $mysqlToday = self::isoToMysql( $today );
  620. if ( ( isset( $mysqlStartDate ) && isset( $mysqlEndDate ) ) && ( ( $mysqlToday >= $mysqlStartDate ) && ( $mysqlToday <= $mysqlEndDate ) ) ){
  621. return true;
  622. } elseif ( ( isset( $mysqlStartDate ) && ! isset( $mysqlEndDate ) ) && ( ( $mysqlToday >= $mysqlStartDate ) ) ) {
  623. return true;
  624. } elseif ( ( ! isset( $mysqlStartDate ) && isset( $mysqlEndDate ) ) && ( ( $mysqlToday <= $mysqlEndDate ) ) ) {
  625. return true;
  626. }
  627. return false;
  628. }
  629. /**
  630. * Function to calculate Age in Years if greater than one year else in months
  631. *
  632. * @param date $birthDate Birth Date
  633. *
  634. * @return int array $results contains years or months
  635. * @access public
  636. */
  637. public function calculateAge($birthDate)
  638. {
  639. $results = array( );
  640. $formatedBirthDate = CRM_Utils_Date::customFormat($birthDate,'%Y-%m-%d');
  641. $bDate = explode('-',$formatedBirthDate);
  642. $birthYear = $bDate[0];
  643. $birthMonth = $bDate[1];
  644. $birthDay = $bDate[2];
  645. $year_diff = date("Y") - $birthYear;
  646. // don't calculate age CRM-3143
  647. if ( $birthYear == '1902' ) {
  648. return $results;
  649. }
  650. switch ($year_diff) {
  651. case 1:
  652. $month = (12 - $birthMonth) + date("m");
  653. if ( $month < 12 ) {
  654. if (date("d") < $birthDay) {
  655. $month--;
  656. }
  657. $results['months'] = $month;
  658. } elseif ( $month == 12 && (date("d") < $birthDay) ) {
  659. $results['months'] = $month-1;
  660. } else {
  661. $results['years'] = $year_diff;
  662. }
  663. break;
  664. case 0:
  665. $month = date("m") - $birthMonth;
  666. $results['months'] = $month;
  667. break;
  668. default:
  669. $results['years'] = $year_diff;
  670. if ( ( date("m") < $birthMonth ) || ( date("m") == $birthMonth ) && ( date("d") < $birthDay ) ) {
  671. $results['years']--;
  672. }
  673. }
  674. return $results;
  675. }
  676. /**
  677. * Function to calculate next payment date according to provided unit & interval
  678. *
  679. * @param string $unit frequency unit like year,month, week etc..
  680. *
  681. * @param int $interval frequency interval.
  682. *
  683. * @param array $date start date of pledge.
  684. *
  685. * @return array $result contains new date with added interval
  686. * @access public
  687. */
  688. function intervalAdd($unit, $interval, $date, $dontCareTime = false )
  689. {
  690. if ( is_array( $date ) ) {
  691. $hour = CRM_Utils_Array::value( 'H', $date );
  692. $minute = CRM_Utils_Array::value( 'i', $date );
  693. $second = CRM_Utils_Array::value( 's', $date );
  694. $month = CRM_Utils_Array::value( 'M', $date );
  695. $day = CRM_Utils_Array::value( 'd', $date );
  696. $year = CRM_Utils_Array::value( 'Y', $date );
  697. } else {
  698. extract( date_parse( $date ) );
  699. }
  700. $date = mktime ($hour, $minute, $second, $month, $day, $year);
  701. switch ( $unit ) {
  702. case 'year':
  703. $date = mktime ($hour, $minute, $second, $month, $day, $year+$interval);
  704. break;
  705. case 'month':
  706. $date = mktime ($hour, $minute, $second, $month+$interval, $day, $year);
  707. break;
  708. case 'week':
  709. $interval = $interval * 7;
  710. $date = mktime ($hour, $minute, $second, $month, $day+$interval, $year);
  711. break;
  712. case 'day':
  713. $date = mktime ($hour, $minute, $second, $month, $day+$interval, $year);
  714. break;
  715. case 'second':
  716. $date = mktime ($hour, $minute, $second+$interval, $month, $day, $year);
  717. break;
  718. }
  719. $scheduleDate = explode ( "-", date("n-j-Y-H-i-s", $date ) );
  720. $date = array( );
  721. $date['M'] = $scheduleDate[0];
  722. $date['d'] = $scheduleDate[1];
  723. $date['Y'] = $scheduleDate[2];
  724. if ( $dontCareTime == false) {
  725. $date['H'] = $scheduleDate[3];
  726. $date['i'] = $scheduleDate[4];
  727. $date['s'] = $scheduleDate[5];
  728. }
  729. return $date;
  730. }
  731. /**
  732. * function to check given format is valid for bith date.
  733. * and retrun supportable birth date format w/ qf mapping.
  734. *
  735. * @param $format given format ( eg 'M Y', 'Y M' )
  736. * return array of qfMapping and date parts for date format.
  737. */
  738. function checkBirthDateFormat( $format = null )
  739. {
  740. $birthDateFormat = null;
  741. if ( !$format ) {
  742. $birthDateFormat = self::getDateFormat( 'birth');
  743. }
  744. $supportableFormats = array(
  745. 'mm/dd' => '%B %E%f',
  746. 'dd-mm' => '%E%f %B',
  747. 'yy-mm' => '%Y %B',
  748. 'M yy' => '%b %Y',
  749. 'yy' => '%Y',
  750. 'dd/mm/yy' => '%E%f %B %Y',
  751. );
  752. if ( array_key_exists( $birthDateFormat, $supportableFormats ) ) {
  753. $birthDateFormat = array( 'qfMapping' => $supportableFormats[$birthDateFormat] );
  754. }
  755. return $birthDateFormat;
  756. }
  757. /**
  758. * resolves the given relative time interval into finite time limits
  759. *
  760. * @param array $relativeTerm relative time frame like this, previous, etc
  761. * @param int $unit frequency unit like year, month, week etc..
  762. * @return array $dateRange start date and end date for the relative time frame
  763. * @static
  764. */
  765. function relativeToAbsolute( $relativeTerm, $unit )
  766. {
  767. $now = getDate();
  768. $from = $to = $dateRange = array();
  769. $from['H'] = $from['i'] = $from['s'] = 0;
  770. switch( $unit ) {
  771. case 'year':
  772. switch( $relativeTerm ) {
  773. case 'this':
  774. $from['d'] = $from['M'] = 1;
  775. $to['d'] = 31;
  776. $to['M'] = 12;
  777. $to['Y'] = $from['Y'] = $now['year'];
  778. break;
  779. case 'previous':
  780. $from['M'] = $from['d'] = 1;
  781. $to['d'] = 31;
  782. $to['M'] = 12;
  783. $to['Y'] = $from['Y'] = $now['year'] - 1;
  784. break;
  785. case 'previous_before':
  786. $from['M'] = $from['d'] = 1;
  787. $to['d'] = 31;
  788. $to['M'] = 12;
  789. $to['Y'] = $from['Y'] = $now['year'] - 2;
  790. break;
  791. case 'previous_2':
  792. $from['M'] = $from['d'] = 1;
  793. $to['d'] = 31;
  794. $to['M'] = 12;
  795. $from['Y'] = $now['year'] - 2;
  796. $to['Y'] = $now['year'] - 1;
  797. break;
  798. case 'earlier':
  799. $to['d'] = 31;
  800. $to['M'] = 12;
  801. $to['Y'] = $now['year'] - 1;
  802. unset($from);
  803. break;
  804. case 'greater':
  805. $from['M'] = $from['d'] = 1;
  806. $from['Y'] = $now['year'];
  807. unset($to);
  808. break;
  809. case 'ending':
  810. $to['d'] = $now['mday'];
  811. $to['M'] = $now['mon'];
  812. $to['Y'] = $now['year'];
  813. $to['H'] = 23;
  814. $to['i'] = $to['s'] = 59;
  815. $from = self::intervalAdd( 'year', -1, $to );
  816. $from = self::intervalAdd( 'second', 1, $from );
  817. break;
  818. }
  819. break;
  820. case 'fiscal_year':
  821. $config = CRM_Core_Config::singleton();
  822. $from['d'] = $config->fiscalYearStart['d'];
  823. $from['M'] = $config->fiscalYearStart['M'];
  824. $fYear = self::calculateFiscalYear( $from['d'],$from['M'] );
  825. switch( $relativeTerm ) {
  826. case 'this':
  827. $from['Y'] = $fYear;
  828. $fiscalYear= mktime(0,0,0,$from['M'],$form['d'],$from['Y']+1);
  829. $fiscalEnd = explode('-',date("Y-m-d", $fiscalYear));
  830. $to['d'] = $fiscalEnd['2'];
  831. $to['M'] = $fiscalEnd['1'];
  832. $to['Y'] = $fiscalEnd['0'];
  833. break;
  834. case 'previous':
  835. $from['Y'] = $fYear - 1;
  836. $fiscalYear= mktime(0,0,0,$from['M'],$form['d'],$from['Y']+1);
  837. $fiscalEnd = explode('-',date("Y-m-d", $fiscalYear));
  838. $to['d'] = $fiscalEnd['2'];
  839. $to['M'] = $fiscalEnd['1'];
  840. $to['Y'] = $fiscalEnd['0'];
  841. break;
  842. }
  843. break;
  844. case 'quarter':
  845. switch( $relativeTerm ) {
  846. case 'this':
  847. $quarter = ceil ( $now['mon'] / 3 );
  848. $from['d'] = 1;
  849. $from['M'] = (3 * $quarter ) - 2;
  850. $to['M'] = 3 * $quarter;
  851. $to['Y'] = $from['Y'] = $now['year'];
  852. $to['d'] = cal_days_in_month(CAL_GREGORIAN, $to['M'], $now['year']);
  853. break;
  854. case 'previous':
  855. $difference = 1;
  856. $quarter = ceil ( $now['mon'] / 3 );
  857. $quarter = $quarter - $difference;
  858. $subtractYear = 0;
  859. if ( $quarter <= 0 ) {
  860. $subtractYear = 1;
  861. $quarter += 4;
  862. }
  863. $from['d'] = 1;
  864. $from['M'] = (3 * $quarter ) - 2;
  865. $to['M'] = 3 * $quarter;
  866. $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
  867. $to['d'] = cal_days_in_month(CAL_GREGORIAN, $to['M'], $to['Y']);
  868. break;
  869. case 'previous_before':
  870. $difference = 2;
  871. $quarter = ceil( $now['mon'] / 3 );
  872. $quarter = $quarter - $difference;
  873. if ( $quarter <= 0 ) {
  874. $subtractYear = 1;
  875. $quarter += 4;
  876. srst; }
  877. $from['d'] = 1;
  878. $from['M'] = (3 * $quarter ) - 2;
  879. $to['M'] = 3 * $quarter;
  880. $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
  881. $to['d'] = cal_days_in_month(CAL_GREGORIAN, $to['M'], $to['Y'] );
  882. break;
  883. case 'previous_2':
  884. $difference = 2;
  885. $quarter = ceil( $now['mon'] / 3 );
  886. $current_quarter = $quarter;
  887. $quarter = $quarter - $difference;
  888. $subtractYear = 0;
  889. if ( $quarter <= 0 ) {
  890. $subtractYear = 1;
  891. $quarter += 4;
  892. }
  893. $from['d'] = 1;
  894. $from['M'] = (3 * $quarter ) - 2;
  895. switch ( $current_quarter ) {
  896. case 1:
  897. $to['M'] = ( 4 * $quarter );
  898. break;
  899. case 2:
  900. $to['M'] = ( 4 * $quarter ) + 3;
  901. break;
  902. case 3:
  903. $to['M'] = ( 4 * $quarter ) + 2;
  904. break;
  905. case 4:
  906. $to['M'] = ( 4 * $quarter ) + 1;
  907. break;
  908. }
  909. $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
  910. if ( $to['M'] > 12 ) {
  911. $to['M'] = 3 * ($quarter - 3);
  912. $to['Y'] = $now['year'];
  913. }
  914. $to['d'] = cal_days_in_month(CAL_GREGORIAN, $to['M'], $to['Y'] );
  915. break;
  916. case 'earlier':
  917. $quarter = ceil ( $now['mon'] / 3) - 1;
  918. if ( $quarter <= 0 ) {
  919. $subtractYear = 1;
  920. $quarter += 4;
  921. }
  922. $to['M'] = 3 * $quarter;
  923. $to['Y'] = $from['Y'] = $now['year'] - $subtractYear;
  924. $to['d'] = cal_days_in_month(CAL_GREGORIAN, $to['M'], $to['Y']);
  925. unset($from);
  926. break;
  927. case 'greater':
  928. $quarter = ceil ( $now['mon'] / 3 );
  929. $from['d'] = 1;
  930. $from['M'] = (3 * $quarter ) - 2;
  931. $from['Y'] = $now['year'];
  932. unset($to);
  933. break;
  934. case 'ending':
  935. $to['d'] = $now['mday'];
  936. $to['M'] = $now['mon'];
  937. $to['Y'] = $now['year'];
  938. $to['H'] = 23;
  939. $to['i'] = $to['s'] = 59;
  940. $from = self::intervalAdd( 'month', -3, $to );
  941. $from = self::intervalAdd( 'second', 1, $from );
  942. break;
  943. }
  944. break;
  945. case 'month':
  946. switch( $relativeTerm ) {
  947. case 'this':
  948. $from['d'] = 1;
  949. $to['d'] = cal_days_in_month(CAL_GREGORIAN, $now['mon'], $now['year']);
  950. $from['M'] = $to['M'] = $now['mon'];
  951. $from['Y'] = $to['Y'] = $now['year'];
  952. break;
  953. case 'previous':
  954. $from['d'] = 1;
  955. if ( $now['mon'] == 1 ) {
  956. $from['M'] = $to['M'] = 12;
  957. $from['Y'] = $to['Y'] = $now['year'] - 1;
  958. } else {
  959. $from['M'] = $to['M'] = $now['mon'] - 1;
  960. $from['Y'] = $to['Y'] = $now['year'];
  961. }
  962. $to['d'] = cal_days_in_month(CAL_GREGORIAN, $to['M'], $to['Y']);
  963. break;
  964. case 'previous_before':
  965. $from['d'] = 1;
  966. if ( $now['mon'] < 3 ) {
  967. $from['M'] = $to['M'] = 10 + $now['mon'];
  968. $from['Y'] = $to['Y'] = $now['year'] - 1;
  969. } else {
  970. $from['M'] = $to['M'] = $now['mon'] - 2;
  971. $from['Y'] = $to['Y'] = $now['year'];
  972. }
  973. $to['d'] = cal_days_in_month(CAL_GREGORIAN, $to['M'], $to['Y']);
  974. break;
  975. case 'previous_2':
  976. $from['d'] = 1;
  977. if ( $now['mon'] < 3 ) {
  978. $from['M'] = 10 + $now['mon'];
  979. $from['Y'] = $now['year'] - 1;
  980. } else {
  981. $from['M'] = $now['mon'] - 2;
  982. $from['Y'] = $now['year'];
  983. }
  984. if ( $now['mon'] == 1 ) {
  985. $to['M'] = 12;
  986. $to['Y'] = $now['year'] - 1;
  987. } else {
  988. $to['M'] = $now['mon'] - 1;
  989. $to['Y'] = $now['year'];
  990. }
  991. $to['d'] = cal_days_in_month(CAL_GREGORIAN, $to['M'], $to['Y']);
  992. break;
  993. case 'earlier':
  994. //before end of past month
  995. if ( $now['mon'] == 1 ) {
  996. $to['M'] = 12;
  997. $to['Y'] = $now['year'] - 1;
  998. } else {
  999. $to['M'] = $now['mon'] - 1;
  1000. $to['Y'] = $now['year'];
  1001. }
  1002. $to['d'] = cal_days_in_month(CAL_GREGORIAN, $to['M'], $to['Y']);
  1003. unset($from);
  1004. break;
  1005. case 'greater':
  1006. $from['d'] = 1;
  1007. $from['M'] = $now['mon'];;
  1008. $from['Y'] = $now['year'];
  1009. unset($to);
  1010. break;
  1011. case 'ending':
  1012. $to['d'] = $now['mday'];
  1013. $to['M'] = $now['mon'];
  1014. $to['Y'] = $now['year'];
  1015. $to['H'] = 23;
  1016. $to['i'] = $to['s'] = 59;
  1017. $from = self::intervalAdd( 'month', -1, $to );
  1018. $from = self::intervalAdd( 'second', 1, $from );
  1019. break;
  1020. }
  1021. break;
  1022. case 'week':
  1023. switch( $relativeTerm ) {
  1024. case 'this':
  1025. $from['d'] = $now['mday'];
  1026. $from['M'] = $now['mon'];
  1027. $from['Y'] = $now['year'];
  1028. $from = self::intervalAdd( 'day', -1*($now['wday']), $from );
  1029. $to = self::intervalAdd( 'day', 6, $from );
  1030. break;
  1031. case 'previous':
  1032. $from['d'] = $now['mday'];
  1033. $from['M'] = $now['mon'];
  1034. $from['Y'] = $now['year'];
  1035. $from = self::intervalAdd( 'day', -1*($now['wday'])-7, $from );
  1036. $to = self::intervalAdd( 'day', 6, $from );
  1037. break;
  1038. case 'previous_before':
  1039. $from['d'] = $now['mday'];
  1040. $from['M'] = $now['mon'];
  1041. $from['Y'] = $now['year'];
  1042. $from = self::intervalAdd( 'day', -1*($now['wday'])-14, $from );
  1043. $to = self::intervalAdd( 'day', 6, $from );
  1044. break;
  1045. case 'previous_2':
  1046. $from['d'] = $now['mday'];
  1047. $from['M'] = $now['mon'];
  1048. $from['Y'] = $now['year'];
  1049. $from = self::intervalAdd( 'day', -1*($now['wday'])-14, $from );
  1050. $to = self::intervalAdd( 'day', 13, $from );
  1051. break;
  1052. case 'earlier':
  1053. $to['d'] = $now['mday'];
  1054. $to['M'] = $now['mon'];
  1055. $to['Y'] = $now['year'];
  1056. $to = self::intervalAdd( 'day', -1*($now['wday'])-1, $to );
  1057. unset($from);
  1058. break;
  1059. case 'greater':
  1060. $from['d'] = $now['mday'];
  1061. $from['M'] = $now['mon'];
  1062. $from['Y'] = $now['year'];
  1063. $from = self::intervalAdd( 'day', -1*($now['wday']), $from );
  1064. unset($to);
  1065. break;
  1066. case 'ending':
  1067. $to['d'] = $now['mday'];
  1068. $to['M'] = $now['mon'];
  1069. $to['Y'] = $now['year'];
  1070. $to['H'] = 23;
  1071. $to['i'] = $to['s'] = 59;
  1072. $from = self::intervalAdd( 'day', -7, $to );
  1073. $from = self::intervalAdd( 'second', 1, $from );
  1074. break;
  1075. }
  1076. break;
  1077. case 'day':
  1078. switch( $relativeTerm ) {
  1079. case 'this':
  1080. $from['d'] = $to['d'] = $now['mday'];
  1081. $from['M'] = $to['M'] = $now['mon'];
  1082. $from['Y'] = $to['Y'] = $now['year'];
  1083. break;
  1084. case 'previous':
  1085. $from['d'] = $now['mday'];
  1086. $from['M'] = $now['mon'];
  1087. $from['Y'] = $now['year'];
  1088. $from = self::intervalAdd( 'day', -1, $from );
  1089. $to['d'] = $from['d'];
  1090. $to['M'] = $from['M'];
  1091. $to['Y'] = $from['Y'];
  1092. break;
  1093. case 'previous_before':
  1094. $from['d'] = $now['mday'];
  1095. $from['M'] = $now['mon'];
  1096. $from['Y'] = $now['year'];
  1097. $from = self::intervalAdd( 'day', -2, $from );
  1098. $to['d'] = $from['d'];
  1099. $to['M'] = $from['M'];
  1100. $to['Y'] = $from['Y'];
  1101. break;
  1102. case 'previous_2':
  1103. $from['d'] = $to['d'] = $now['mday'];
  1104. $from['M'] = $to['M'] = $now['mon'];
  1105. $from['Y'] = $to['Y'] = $now['year'];
  1106. $from = self::intervalAdd( 'day', -2, $from );
  1107. $to = self::intervalAdd( 'day', -1, $to );
  1108. break;
  1109. case 'earlier':
  1110. $to['d'] = $now['mday'];
  1111. $to['M'] = $now['mon'];
  1112. $to['Y'] = $now['year'];
  1113. unset($from);
  1114. break;
  1115. case 'greater':
  1116. $from['d'] = $now['mday'];
  1117. $from['M'] = $now['mon'];;
  1118. $from['Y'] = $now['year'];
  1119. unset($to);
  1120. break;
  1121. }
  1122. break;
  1123. }
  1124. foreach ( array( 'from', 'to' ) as $item ) {
  1125. if ( !empty ( $$item ) ) {
  1126. $dateRange[$item] = self::format( $$item );
  1127. } else {
  1128. $dateRange[$item] = null;
  1129. }
  1130. }
  1131. return $dateRange;
  1132. }
  1133. /**
  1134. * Function to calculate current fiscal year based on the fiscal month and day
  1135. *
  1136. * @param int $fyDate Fiscal start date
  1137. *
  1138. * @param int $fyMonth Fiscal Start Month
  1139. *
  1140. * @return int $fy Current Fiscl Year
  1141. * @access public
  1142. */
  1143. function calculateFiscalYear( $fyDate, $fyMonth ) {
  1144. $date = date("Y-m-d");
  1145. $currentYear = date("Y");
  1146. //recalculate the date because month 4::04 make the difference
  1147. $fiscalYear = explode('-', date( "Y-m-d", mktime( 0,0,0, $fyMonth, $fyDate, $currentYear ) ) );
  1148. $fyDate = $fiscalYear[2];
  1149. $fyMonth = $fiscalYear[1];
  1150. $fyStartDate = date("Y-m-d", mktime( 0,0,0, $fyMonth, $fyDate, $currentYear ));
  1151. if ( $fyStartDate > $date ) {
  1152. $fy = intval(intval($currentYear) - 1 );
  1153. } else {
  1154. $fy = intval($currentYear);
  1155. }
  1156. return $fy;
  1157. }
  1158. /**
  1159. * Function to process date, convert to mysql format
  1160. *
  1161. * @param string $date date string
  1162. * @param string $time time string
  1163. * @param string $returnNullString 'null' needs to be returned
  1164. * so that db oject will set null in db
  1165. * @param string $format expected return date format.( default is mysql )
  1166. *
  1167. * @return string $mysqlDate date format that is excepted by mysql
  1168. */
  1169. static function processDate( $date, $time = null, $returnNullString = false, $format = 'YmdHis', $inputCustomFormat = null ) {
  1170. $mysqlDate = null;
  1171. if ( $returnNullString ) {
  1172. $mysqlDate = 'null';
  1173. }
  1174. $config = CRM_Core_Config::singleton( );
  1175. $inputFormat = $config->dateInputFormat;
  1176. if ( !empty( $inputCustomFormat ) ) {
  1177. $inputFormat = $inputCustomFormat;
  1178. }
  1179. if ( trim( $date ) ) {
  1180. $mysqlDate = date( $format, strtotime( $date . ' '. $time ) );
  1181. }
  1182. return $mysqlDate;
  1183. }
  1184. /**
  1185. * Function to convert mysql to date plugin format
  1186. *
  1187. * @param string $mysqlDate date string
  1188. *
  1189. * @return array $date and time
  1190. */
  1191. static function setDateDefaults( $mysqlDate = null, $formatType = null, $format = null, $timeFormat = null ) {
  1192. // if date is not passed assume it as today
  1193. if ( !$mysqlDate ) {
  1194. $mysqlDate = date( 'Y-m-d G:i:s' ) ;
  1195. }
  1196. $config = CRM_Core_Config::singleton();
  1197. if ( $formatType ) {
  1198. // get actual format
  1199. $params = array( 'name' => $formatType );
  1200. $values = array( );
  1201. CRM_Core_DAO::commonRetrieve( 'CRM_Core_DAO_PreferencesDate', $params, $values );
  1202. if ( $values['date_format'] ) {
  1203. $format = $values['date_format'];
  1204. }
  1205. if ( $values['time_format'] ) {
  1206. $timeFormat = $values['time_format'];
  1207. }
  1208. }
  1209. if ( !$format ) {
  1210. $format = $config->dateInputFormat;
  1211. }
  1212. require_once 'CRM/Core/SelectValues.php';
  1213. // get actual format
  1214. $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats( );
  1215. $dateFormat = CRM_Utils_Array::value( $format, $actualPHPFormats );
  1216. $date = date( $dateFormat, strtotime( $mysqlDate) );
  1217. if ( !$timeFormat ) {
  1218. $timeFormat = $config->timeInputFormat;
  1219. }
  1220. $actualTimeFormat = "g:iA";
  1221. $appendZeroLength = 7;
  1222. if ( $timeFormat > 1 ) {
  1223. $actualTimeFormat = "G:i";
  1224. $appendZeroLength = 5;
  1225. }
  1226. $time = date( $actualTimeFormat, strtotime( $mysqlDate) );
  1227. // need to append zero for hours < 10
  1228. if ( strlen( $time) < $appendZeroLength ) {
  1229. $time = '0' . $time;
  1230. }
  1231. return array( $date, $time );
  1232. }
  1233. /**
  1234. * Function get date format
  1235. * @param string $formatType Date name e.g. birth
  1236. *
  1237. * @return string $format
  1238. */
  1239. static function getDateFormat( $formatType = null ) {
  1240. $format = null;
  1241. if ( $formatType ) {
  1242. $format = CRM_Core_Dao::getFieldValue( 'CRM_Core_DAO_PreferencesDate',
  1243. $formatType, 'date_format', 'name' );
  1244. }
  1245. if ( !$format ) {
  1246. $config = CRM_Core_Config::singleton();
  1247. $format = $config->dateInputFormat;
  1248. }
  1249. return $format;
  1250. }
  1251. }