Merge pull request #22277 from demeritcowboy/isdir2
[civicrm-core.git] / CRM / Core / Smarty / plugins / modifier.crmDate.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Convert the date string "YYYY-MM-DD" to "MM<long> DD, YYYY".
20 *
21 * @param string $dateString
22 * Date which needs to converted to human readable format.
23 *
24 * @param string|null $dateFormat
25 * A string per https://www.php.net/manual/en/function.strftime.php or
26 * one of our configured formats name - eg
27 * - dateformatDatetime
28 * - dateformatFull
29 * - dateformatPartial
30 * - dateformatTime
31 * - dateformatYear
32 * - dateformatFinancialBatch
33 * - dateformatshortdate
34 *
35 * @param bool $onlyTime
36 *
37 * @return string
38 * human readable date format | invalid date message
39 */
40 function smarty_modifier_crmDate($dateString, ?string $dateFormat = NULL, bool $onlyTime = FALSE): string {
41 if ($dateString) {
42 $configuredFormats = [
43 'Datetime',
44 'Full',
45 'Partial',
46 'Time',
47 'Year',
48 'FinancialBatch',
49 'shortdate',
50 ];
51 if (in_array($dateFormat, $configuredFormats, TRUE)) {
52 $dateFormat = Civi::settings()->get('dateformat' . $dateFormat);
53 }
54 // this check needs to be type sensitive
55 // CRM-3689, CRM-2441
56 if ($dateFormat === 0) {
57 $dateFormat = NULL;
58 }
59 if ($onlyTime) {
60 $config = CRM_Core_Config::singleton();
61 $dateFormat = $config->dateformatTime;
62 }
63
64 return CRM_Utils_Date::customFormat($dateString, $dateFormat);
65 }
66 return '';
67 }