chore: increment v_js_includes (#7029)
[openemr.git] / library / formatting.inc.php
blob257cabfaf703b375fd43e1813007f453b07897df
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\Common\Utils\FormatMoney;
16 use OpenEMR\Services\Utils\DateFormatterUtils;
18 // TODO: look at moving all of the date functions into the DateFormatterUtils class.
20 function oeFormatMoney($amount, $symbol = false)
22 return FormatMoney::getFormattedMoney($amount, $symbol);
25 function oeFormatShortDate($date = 'today', $showYear = true)
27 return DateFormatterUtils::oeFormatShortDate($date, $showYear);
30 // 0 - Time format 24 hr
31 // 1 - Time format 12 hr
32 function oeFormatTime($time, $format = "global", $seconds = false)
34 if (empty($time)) {
35 return "";
38 $formatted = $time;
40 if ($format === "global") {
41 $format = $GLOBALS['time_display_format'];
45 if ($format == 1) {
46 if ($seconds) {
47 $formatted = date("g:i:s a", strtotime($time));
48 } else {
49 $formatted = date("g:i a", strtotime($time));
51 } else { // ($format == 0)
52 if ($seconds) {
53 $formatted = date("H:i:s", strtotime($time));
54 } else {
55 $formatted = date("H:i", strtotime($time));
59 return $formatted;
62 /**
63 * Returns the complete formatted datetime string according the global date and time format
64 * @param $datetime
65 * @return string
67 function oeFormatDateTime($datetime, $formatTime = "global", $seconds = false)
69 return oeFormatShortDate(substr($datetime ?? '', 0, 10)) . " " . oeFormatTime(substr($datetime ?? '', 11), $formatTime, $seconds);
72 /**
73 * Returns the complete formatted datetime string according the global date and time format
74 * @param $timestamp
75 * @return string
77 function oeTimestampFormatDateTime($timestamp)
79 if (!$timestamp) {
80 $timestamp = strtotime(date('Y-m-d H:i'));
83 if ($GLOBALS['time_display_format'] == 0) {
84 $timeFormat = 'H:i';
85 } else { // $GLOBALS['time_display_format'] == 1
86 $timeFormat = 'g:i a';
89 if ($GLOBALS['date_display_format'] == 1) { // mm/dd/yyyy
90 $newDate = date('m/d/Y ' . $timeFormat, $timestamp);
91 } elseif ($GLOBALS['date_display_format'] == 2) { // dd/mm/yyyy
92 $newDate = date('d/m/Y ' . $timeFormat, $timestamp);
93 } else { // yyyy-mm-dd
94 $newDate = date('Y-m-d ' . $timeFormat, $timestamp);
97 return $newDate;
100 // Format short date from time.
101 function oeFormatSDFT($time)
103 return oeFormatShortDate(date('Y-m-d', $time));
106 // Format the body of a patient note.
107 function oeFormatPatientNote($note)
109 $i = 0;
110 while ($i !== false) {
111 if (preg_match('/^\d\d\d\d-\d\d-\d\d/', substr($note, $i))) {
112 $note = substr($note, 0, $i) . oeFormatShortDate(substr($note, $i, 10)) . substr($note, $i + 10);
115 $i = strpos($note, "\n", $i);
116 if ($i !== false) {
117 ++$i;
121 return $note;
124 function oeFormatClientID($id)
127 // TBD
129 return $id;
131 //----------------------------------------------------
132 function DateFormatRead($mode = 'legacy')
134 //For the 3 supported date format,the javascript code also should be twicked to display the date as per it.
135 //Output of this function is given to 'ifFormat' parameter of the 'Calendar.setup'.
136 //This will show the date as per the global settings.
137 if ($GLOBALS['date_display_format'] == 0) {
138 if ($mode == 'legacy') {
139 return "%Y-%m-%d";
140 } elseif ($mode == 'validateJS') {
141 return "YYYY-MM-DD";
142 } else { //$mode=='jquery-datetimepicker'
143 return "Y-m-d";
145 } elseif ($GLOBALS['date_display_format'] == 1) {
146 if ($mode == 'legacy') {
147 return "%m/%d/%Y";
148 } elseif ($mode == 'validateJS') {
149 return "MM/DD/YYYY";
150 } else { //$mode=='jquery-datetimepicker'
151 return "m/d/Y";
153 } elseif ($GLOBALS['date_display_format'] == 2) {
154 if ($mode == 'legacy') {
155 return "%d/%m/%Y";
156 } elseif ($mode == 'validateJS') {
157 return "DD/MM/YYYY";
158 } else { //$mode=='jquery-datetimepicker'
159 return "d/m/Y";
164 function DateToYYYYMMDD($DateValue)
166 return DateFormatterUtils::DateToYYYYMMDD($DateValue);
169 function TimeToHHMMSS($TimeValue)
171 if (trim($TimeValue) == '') {
172 return '';
175 $date = new DateTimeImmutable('1970-01-01' . $TimeValue);
176 return $date->format('H:i:s');
180 function DateTimeToYYYYMMDDHHMMSS($DateTimeValue)
182 //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.
184 // First deal with the date
185 $fixed_date = DateToYYYYMMDD(substr($DateTimeValue, 0, 10));
187 // Then deal with the time
188 $fixed_time = TimeToHHMMSS(substr($DateTimeValue, 11));
190 if (empty($fixed_date) && empty($fixed_time)) {
191 return "";
192 } else {
193 return $fixed_date . " " . $fixed_time;
197 // Returns age in a desired format:
198 // 0 = "xx month(s)" if < 2 years, else years
199 // 1 = Years : just a number
200 // 2 = Months : just a number
201 // 3 = Gestational: "xx week(s) y day(s)"
202 // $dobYMD is YYYYMMDD or YYYY-MM-DD
203 // $nowYMD is same format but optional
205 function oeFormatAge($dobYMD, $nowYMD = '', $format = 0)
207 // Strip any dashes from the dates.
208 $dobYMD = preg_replace('/-/', '', $dobYMD);
209 $nowYMD = preg_replace('/-/', '', $nowYMD);
210 $dobDay = substr($dobYMD, 6, 2);
211 $dobMonth = substr($dobYMD, 4, 2);
212 $dobYear = substr($dobYMD, 0, 4);
214 if ($nowYMD) {
215 $nowDay = substr($nowYMD, 6, 2);
216 $nowMonth = substr($nowYMD, 4, 2);
217 $nowYear = substr($nowYMD, 0, 4);
218 } else {
219 $nowDay = date("d");
220 $nowMonth = date("m");
221 $nowYear = date("Y");
224 if ($format == 3) {
225 // Gestational age as weeks and days.
226 $secs = mktime(0, 0, 0, $nowMonth, $nowDay, $nowYear) -
227 mktime(0, 0, 0, $dobMonth, $dobDay, $dobYear);
228 $days = intval($secs / (24 * 60 * 60));
229 $weeks = intval($days / 7);
230 $days = $days % 7;
231 $age = "$weeks " . ($weeks == 1 ? xl('week') : xl('weeks')) .
232 " $days " . ($days == 1 ? xl('day') : xl('days'));
233 } else {
234 // Years or months.
235 $dayDiff = $nowDay - $dobDay;
236 $monthDiff = $nowMonth - $dobMonth;
237 $yearDiff = $nowYear - $dobYear;
238 $ageInMonths = $yearDiff * 12 + $monthDiff;
239 if ($dayDiff < 0) {
240 --$ageInMonths;
243 if ($format == 1 || ($format == 0 && $ageInMonths >= 24)) {
244 $age = $yearDiff;
245 if ($monthDiff < 0 || ($monthDiff == 0 && $dayDiff < 0)) {
246 --$age;
248 } else {
249 $age = $ageInMonths;
250 if ($format == 0) {
251 $age .= ' ' . $ageInMonths == 1 ? xl('month') : xl('months');
256 return $age;