Bug fix (#6333)
[openemr.git] / library / formatting.inc.php
blobce344b0bccf27d4300e8f37335d3be2903fad7d0
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 $is_pm = (stripos($TimeValue, 'PM') !== false);
187 if ($is_pm) {
188 $dt = new DateTime('1970-01-01' . $TimeValue);
189 $TimeValue = $dt->modify('+12 hours')->format('H:i:s');
192 return $TimeValue;
196 function DateTimeToYYYYMMDDHHMMSS($DateTimeValue)
198 //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.
200 // First deal with the date
201 $fixed_date = DateToYYYYMMDD(substr($DateTimeValue, 0, 10));
203 // Then deal with the time
204 $fixed_time = TimeToHHMMSS(substr($DateTimeValue, 11));
206 if (empty($fixed_date) && empty($fixed_time)) {
207 return "";
208 } else {
209 return $fixed_date . " " . $fixed_time;
213 // Returns age in a desired format:
214 // 0 = "xx month(s)" if < 2 years, else years
215 // 1 = Years : just a number
216 // 2 = Months : just a number
217 // 3 = Gestational: "xx week(s) y day(s)"
218 // $dobYMD is YYYYMMDD or YYYY-MM-DD
219 // $nowYMD is same format but optional
221 function oeFormatAge($dobYMD, $nowYMD = '', $format = 0)
223 // Strip any dashes from the dates.
224 $dobYMD = preg_replace('/-/', '', $dobYMD);
225 $nowYMD = preg_replace('/-/', '', $nowYMD);
226 $dobDay = substr($dobYMD, 6, 2);
227 $dobMonth = substr($dobYMD, 4, 2);
228 $dobYear = substr($dobYMD, 0, 4);
230 if ($nowYMD) {
231 $nowDay = substr($nowYMD, 6, 2);
232 $nowMonth = substr($nowYMD, 4, 2);
233 $nowYear = substr($nowYMD, 0, 4);
234 } else {
235 $nowDay = date("d");
236 $nowMonth = date("m");
237 $nowYear = date("Y");
240 if ($format == 3) {
241 // Gestational age as weeks and days.
242 $secs = mktime(0, 0, 0, $nowMonth, $nowDay, $nowYear) -
243 mktime(0, 0, 0, $dobMonth, $dobDay, $dobYear);
244 $days = intval($secs / (24 * 60 * 60));
245 $weeks = intval($days / 7);
246 $days = $days % 7;
247 $age = "$weeks " . ($weeks == 1 ? xl('week') : xl('weeks')) .
248 " $days " . ($days == 1 ? xl('day') : xl('days'));
249 } else {
250 // Years or months.
251 $dayDiff = $nowDay - $dobDay;
252 $monthDiff = $nowMonth - $dobMonth;
253 $yearDiff = $nowYear - $dobYear;
254 $ageInMonths = $yearDiff * 12 + $monthDiff;
255 if ($dayDiff < 0) {
256 --$ageInMonths;
259 if ($format == 1 || ($format == 0 && $ageInMonths >= 24)) {
260 $age = $yearDiff;
261 if ($monthDiff < 0 || ($monthDiff == 0 && $dayDiff < 0)) {
262 --$age;
264 } else {
265 $age = $ageInMonths;
266 if ($format == 0) {
267 $age .= ' ' . $ageInMonths == 1 ? xl('month') : xl('months');
272 return $age;