chore: ci mariadb stuff - added 11.1 and removed 10.9 (#6834)
[openemr.git] / library / formatting.inc.php
blob880c7fef53c031161a1639d485e4f23a42fa110d
1 <?php
3 /**
4 * Formatting library.
6 * @package OpenEMR
7 * @link https://www.open-emr.org
8 * @author Rod Roark <rod@sunsetsystems.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2010-2014 Rod Roark <rod@sunsetsystems.com>
11 * @copyright Copyright (c) 2017-2018 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 use OpenEMR\Services\Utils\DateFormatterUtils;
17 // TODO: look at moving all of the date functions into the DateFormatterUtils class.
19 function oeFormatMoney($amount, $symbol = false)
21 $s = number_format(
22 floatval($amount),
23 $GLOBALS['currency_decimals'],
24 $GLOBALS['currency_dec_point'],
25 $GLOBALS['currency_thousands_sep']
27 // If the currency symbol exists and is requested, prepend it.
28 if ($symbol && !empty($GLOBALS['gbl_currency_symbol'])) {
29 $s = $GLOBALS['gbl_currency_symbol'] . " $s";
32 return $s;
35 function oeFormatShortDate($date = 'today', $showYear = true)
37 return DateFormatterUtils::oeFormatShortDate($date, $showYear);
40 // 0 - Time format 24 hr
41 // 1 - Time format 12 hr
42 function oeFormatTime($time, $format = "global", $seconds = false)
44 if (empty($time)) {
45 return "";
48 $formatted = $time;
50 if ($format === "global") {
51 $format = $GLOBALS['time_display_format'];
55 if ($format == 1) {
56 if ($seconds) {
57 $formatted = date("g:i:s a", strtotime($time));
58 } else {
59 $formatted = date("g:i a", strtotime($time));
61 } else { // ($format == 0)
62 if ($seconds) {
63 $formatted = date("H:i:s", strtotime($time));
64 } else {
65 $formatted = date("H:i", strtotime($time));
69 return $formatted;
72 /**
73 * Returns the complete formatted datetime string according the global date and time format
74 * @param $datetime
75 * @return string
77 function oeFormatDateTime($datetime, $formatTime = "global", $seconds = false)
79 return oeFormatShortDate(substr($datetime ?? '', 0, 10)) . " " . oeFormatTime(substr($datetime ?? '', 11), $formatTime, $seconds);
82 /**
83 * Returns the complete formatted datetime string according the global date and time format
84 * @param $timestamp
85 * @return string
87 function oeTimestampFormatDateTime($timestamp)
89 if (!$timestamp) {
90 $timestamp = strtotime(date('Y-m-d H:i'));
93 if ($GLOBALS['time_display_format'] == 0) {
94 $timeFormat = 'H:i';
95 } else { // $GLOBALS['time_display_format'] == 1
96 $timeFormat = 'g:i a';
99 if ($GLOBALS['date_display_format'] == 1) { // mm/dd/yyyy
100 $newDate = date('m/d/Y ' . $timeFormat, $timestamp);
101 } elseif ($GLOBALS['date_display_format'] == 2) { // dd/mm/yyyy
102 $newDate = date('d/m/Y ' . $timeFormat, $timestamp);
103 } else { // yyyy-mm-dd
104 $newDate = date('Y-m-d ' . $timeFormat, $timestamp);
107 return $newDate;
110 // Format short date from time.
111 function oeFormatSDFT($time)
113 return oeFormatShortDate(date('Y-m-d', $time));
116 // Format the body of a patient note.
117 function oeFormatPatientNote($note)
119 $i = 0;
120 while ($i !== false) {
121 if (preg_match('/^\d\d\d\d-\d\d-\d\d/', substr($note, $i))) {
122 $note = substr($note, 0, $i) . oeFormatShortDate(substr($note, $i, 10)) . substr($note, $i + 10);
125 $i = strpos($note, "\n", $i);
126 if ($i !== false) {
127 ++$i;
131 return $note;
134 function oeFormatClientID($id)
137 // TBD
139 return $id;
141 //----------------------------------------------------
142 function DateFormatRead($mode = 'legacy')
144 //For the 3 supported date format,the javascript code also should be twicked to display the date as per it.
145 //Output of this function is given to 'ifFormat' parameter of the 'Calendar.setup'.
146 //This will show the date as per the global settings.
147 if ($GLOBALS['date_display_format'] == 0) {
148 if ($mode == 'legacy') {
149 return "%Y-%m-%d";
150 } elseif ($mode == 'validateJS') {
151 return "YYYY-MM-DD";
152 } else { //$mode=='jquery-datetimepicker'
153 return "Y-m-d";
155 } elseif ($GLOBALS['date_display_format'] == 1) {
156 if ($mode == 'legacy') {
157 return "%m/%d/%Y";
158 } elseif ($mode == 'validateJS') {
159 return "MM/DD/YYYY";
160 } else { //$mode=='jquery-datetimepicker'
161 return "m/d/Y";
163 } elseif ($GLOBALS['date_display_format'] == 2) {
164 if ($mode == 'legacy') {
165 return "%d/%m/%Y";
166 } elseif ($mode == 'validateJS') {
167 return "DD/MM/YYYY";
168 } else { //$mode=='jquery-datetimepicker'
169 return "d/m/Y";
174 function DateToYYYYMMDD($DateValue)
176 return DateFormatterUtils::DateToYYYYMMDD($DateValue);
179 function TimeToHHMMSS($TimeValue)
181 if (trim($TimeValue) == '') {
182 return '';
185 $date = new DateTimeImmutable('1970-01-01' . $TimeValue);
186 return $date->format('H:i:s');
190 function DateTimeToYYYYMMDDHHMMSS($DateTimeValue)
192 //This function accepts a timestamp in any of the selected formats, and as per the global setting, converts it to the yyyy-mm-dd hh:mm:ss format.
194 // First deal with the date
195 $fixed_date = DateToYYYYMMDD(substr($DateTimeValue, 0, 10));
197 // Then deal with the time
198 $fixed_time = TimeToHHMMSS(substr($DateTimeValue, 11));
200 if (empty($fixed_date) && empty($fixed_time)) {
201 return "";
202 } else {
203 return $fixed_date . " " . $fixed_time;
207 // Returns age in a desired format:
208 // 0 = "xx month(s)" if < 2 years, else years
209 // 1 = Years : just a number
210 // 2 = Months : just a number
211 // 3 = Gestational: "xx week(s) y day(s)"
212 // $dobYMD is YYYYMMDD or YYYY-MM-DD
213 // $nowYMD is same format but optional
215 function oeFormatAge($dobYMD, $nowYMD = '', $format = 0)
217 // Strip any dashes from the dates.
218 $dobYMD = preg_replace('/-/', '', $dobYMD);
219 $nowYMD = preg_replace('/-/', '', $nowYMD);
220 $dobDay = substr($dobYMD, 6, 2);
221 $dobMonth = substr($dobYMD, 4, 2);
222 $dobYear = substr($dobYMD, 0, 4);
224 if ($nowYMD) {
225 $nowDay = substr($nowYMD, 6, 2);
226 $nowMonth = substr($nowYMD, 4, 2);
227 $nowYear = substr($nowYMD, 0, 4);
228 } else {
229 $nowDay = date("d");
230 $nowMonth = date("m");
231 $nowYear = date("Y");
234 if ($format == 3) {
235 // Gestational age as weeks and days.
236 $secs = mktime(0, 0, 0, $nowMonth, $nowDay, $nowYear) -
237 mktime(0, 0, 0, $dobMonth, $dobDay, $dobYear);
238 $days = intval($secs / (24 * 60 * 60));
239 $weeks = intval($days / 7);
240 $days = $days % 7;
241 $age = "$weeks " . ($weeks == 1 ? xl('week') : xl('weeks')) .
242 " $days " . ($days == 1 ? xl('day') : xl('days'));
243 } else {
244 // Years or months.
245 $dayDiff = $nowDay - $dobDay;
246 $monthDiff = $nowMonth - $dobMonth;
247 $yearDiff = $nowYear - $dobYear;
248 $ageInMonths = $yearDiff * 12 + $monthDiff;
249 if ($dayDiff < 0) {
250 --$ageInMonths;
253 if ($format == 1 || ($format == 0 && $ageInMonths >= 24)) {
254 $age = $yearDiff;
255 if ($monthDiff < 0 || ($monthDiff == 0 && $dayDiff < 0)) {
256 --$age;
258 } else {
259 $age = $ageInMonths;
260 if ($format == 0) {
261 $age .= ' ' . $ageInMonths == 1 ? xl('month') : xl('months');
266 return $age;