minor change to prior commit
[openemr.git] / library / formatting.inc.php
blob23029cc3164d4225f7f87719b3a63041d668bef5
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 function oeFormatMoney($amount, $symbol = false)
17 $s = number_format(
18 floatval($amount),
19 $GLOBALS['currency_decimals'],
20 $GLOBALS['currency_dec_point'],
21 $GLOBALS['currency_thousands_sep']
23 // If the currency symbol exists and is requested, prepend it.
24 if ($symbol && !empty($GLOBALS['gbl_currency_symbol'])) {
25 $s = $GLOBALS['gbl_currency_symbol'] . " $s";
28 return $s;
31 function oeFormatShortDate($date = 'today', $showYear = true)
33 if ($date === 'today') {
34 $date = date('Y-m-d');
37 if (strlen($date) >= 10) {
38 // assume input is yyyy-mm-dd
39 if ($GLOBALS['date_display_format'] == 1) { // mm/dd/yyyy, note year is added below
40 $newDate = substr($date, 5, 2) . '/' . substr($date, 8, 2);
41 } elseif ($GLOBALS['date_display_format'] == 2) { // dd/mm/yyyy, note year is added below
42 $newDate = substr($date, 8, 2) . '/' . substr($date, 5, 2);
45 // process the year (add for formats 1 and 2; remove for format 0)
46 if ($GLOBALS['date_display_format'] == 1 || $GLOBALS['date_display_format'] == 2) {
47 if ($showYear) {
48 $newDate .= '/' . substr($date, 0, 4);
50 } elseif (!$showYear) { // $GLOBALS['date_display_format'] == 0
51 // need to remove the year
52 $newDate = substr($date, 5, 2) . '-' . substr($date, 8, 2);
53 } else { // $GLOBALS['date_display_format'] == 0
54 // keep the year (so will simply be the original $date)
55 $newDate = substr($date, 0, 10);
58 return $newDate;
61 // this is case if the $date does not have 10 characters
62 return $date;
65 // 0 - Time format 24 hr
66 // 1 - Time format 12 hr
67 function oeFormatTime($time, $format = "global", $seconds = false)
69 if (empty($time)) {
70 return "";
73 $formatted = $time;
75 if ($format === "global") {
76 $format = $GLOBALS['time_display_format'];
80 if ($format == 1) {
81 if ($seconds) {
82 $formatted = date("g:i:s a", strtotime($time));
83 } else {
84 $formatted = date("g:i a", strtotime($time));
86 } else { // ($format == 0)
87 if ($seconds) {
88 $formatted = date("H:i:s", strtotime($time));
89 } else {
90 $formatted = date("H:i", strtotime($time));
94 return $formatted;
97 /**
98 * Returns the complete formatted datetime string according the global date and time format
99 * @param $datetime
100 * @return string
102 function oeFormatDateTime($datetime, $formatTime = "global", $seconds = false)
104 return oeFormatShortDate(substr($datetime, 0, 10)) . " " . oeFormatTime(substr($datetime, 11), $formatTime, $seconds);
108 * Returns the complete formatted datetime string according the global date and time format
109 * @param $timestamp
110 * @return string
112 function oeTimestampFormatDateTime($timestamp)
114 if (!$timestamp) {
115 $timestamp = strtotime(date('Y-m-d H:i'));
118 if ($GLOBALS['time_display_format'] == 0) {
119 $timeFormat = 'H:i';
120 } else { // $GLOBALS['time_display_format'] == 1
121 $timeFormat = 'g:i a';
124 if ($GLOBALS['date_display_format'] == 1) { // mm/dd/yyyy
125 $newDate = date('m/d/Y ' . $timeFormat, $timestamp);
126 } elseif ($GLOBALS['date_display_format'] == 2) { // dd/mm/yyyy
127 $newDate = date('d/m/Y ' . $timeFormat, $timestamp);
128 } else { // yyyy-mm-dd
129 $newDate = date('Y-m-d ' . $timeFormat, $timestamp);
132 return $newDate;
135 // Format short date from time.
136 function oeFormatSDFT($time)
138 return oeFormatShortDate(date('Y-m-d', $time));
141 // Format the body of a patient note.
142 function oeFormatPatientNote($note)
144 $i = 0;
145 while ($i !== false) {
146 if (preg_match('/^\d\d\d\d-\d\d-\d\d/', substr($note, $i))) {
147 $note = substr($note, 0, $i) . oeFormatShortDate(substr($note, $i, 10)) . substr($note, $i + 10);
150 $i = strpos($note, "\n", $i);
151 if ($i !== false) {
152 ++$i;
156 return $note;
159 function oeFormatClientID($id)
162 // TBD
164 return $id;
166 //----------------------------------------------------
167 function DateFormatRead($mode = 'legacy')
169 //For the 3 supported date format,the javascript code also should be twicked to display the date as per it.
170 //Output of this function is given to 'ifFormat' parameter of the 'Calendar.setup'.
171 //This will show the date as per the global settings.
172 if ($GLOBALS['date_display_format'] == 0) {
173 if ($mode == 'legacy') {
174 return "%Y-%m-%d";
175 } elseif ($mode == 'validateJS') {
176 return "YYYY-MM-DD";
177 } else { //$mode=='jquery-datetimepicker'
178 return "Y-m-d";
180 } elseif ($GLOBALS['date_display_format'] == 1) {
181 if ($mode == 'legacy') {
182 return "%m/%d/%Y";
183 } elseif ($mode == 'validateJS') {
184 return "MM/DD/YYYY";
185 } else { //$mode=='jquery-datetimepicker'
186 return "m/d/Y";
188 } elseif ($GLOBALS['date_display_format'] == 2) {
189 if ($mode == 'legacy') {
190 return "%d/%m/%Y";
191 } elseif ($mode == 'validateJS') {
192 return "DD/MM/YYYY";
193 } else { //$mode=='jquery-datetimepicker'
194 return "d/m/Y";
199 function DateToYYYYMMDD($DateValue)
201 //With the help of function DateFormatRead() now the user can enter date is any of the 3 formats depending upon the global setting.
202 //But in database the date can be stored only in the yyyy-mm-dd format.
203 //This function accepts a date in any of the 3 formats, and as per the global setting, converts it to the yyyy-mm-dd format.
204 if (trim($DateValue) == '') {
205 return '';
208 if ($GLOBALS['date_display_format'] == 0) {
209 return $DateValue;
210 } elseif ($GLOBALS['date_display_format'] == 1 || $GLOBALS['date_display_format'] == 2) {
211 $DateValueArray = explode('/', $DateValue);
212 if ($GLOBALS['date_display_format'] == 1) {
213 return $DateValueArray[2] . '-' . $DateValueArray[0] . '-' . $DateValueArray[1];
216 if ($GLOBALS['date_display_format'] == 2) {
217 return $DateValueArray[2] . '-' . $DateValueArray[1] . '-' . $DateValueArray[0];
222 function TimeToHHMMSS($TimeValue)
224 //For now, just return the $TimeValue, since input fields are not formatting time.
225 // This can be upgraded if decided to format input time fields.
227 if (trim($TimeValue) == '') {
228 return '';
231 return $TimeValue;
235 function DateTimeToYYYYMMDDHHMMSS($DateTimeValue)
237 //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.
239 // First deal with the date
240 $fixed_date = DateToYYYYMMDD(substr($DateTimeValue, 0, 10));
242 // Then deal with the time
243 $fixed_time = TimeToHHMMSS(substr($DateTimeValue, 11));
245 if (empty($fixed_date) && empty($fixed_time)) {
246 return "";
247 } else {
248 return $fixed_date . " " . $fixed_time;
252 // Returns age in a desired format:
253 // 0 = "xx month(s)" if < 2 years, else years
254 // 1 = Years : just a number
255 // 2 = Months : just a number
256 // 3 = Gestational: "xx week(s) y day(s)"
257 // $dobYMD is YYYYMMDD or YYYY-MM-DD
258 // $nowYMD is same format but optional
260 function oeFormatAge($dobYMD, $nowYMD = '', $format = 0)
262 // Strip any dashes from the dates.
263 $dobYMD = preg_replace('/-/', '', $dobYMD);
264 $nowYMD = preg_replace('/-/', '', $nowYMD);
265 $dobDay = substr($dobYMD, 6, 2);
266 $dobMonth = substr($dobYMD, 4, 2);
267 $dobYear = substr($dobYMD, 0, 4);
269 if ($nowYMD) {
270 $nowDay = substr($nowYMD, 6, 2);
271 $nowMonth = substr($nowYMD, 4, 2);
272 $nowYear = substr($nowYMD, 0, 4);
273 } else {
274 $nowDay = date("d");
275 $nowMonth = date("m");
276 $nowYear = date("Y");
279 if ($format == 3) {
280 // Gestational age as weeks and days.
281 $secs = mktime(0, 0, 0, $nowMonth, $nowDay, $nowYear) -
282 mktime(0, 0, 0, $dobMonth, $dobDay, $dobYear);
283 $days = intval($secs / (24 * 60 * 60));
284 $weeks = intval($days / 7);
285 $days = $days % 7;
286 $age = "$weeks " . ($weeks == 1 ? xl('week') : xl('weeks')) .
287 " $days " . ($days == 1 ? xl('day') : xl('days'));
288 } else {
289 // Years or months.
290 $dayDiff = $nowDay - $dobDay;
291 $monthDiff = $nowMonth - $dobMonth;
292 $yearDiff = $nowYear - $dobYear;
293 $ageInMonths = $yearDiff * 12 + $monthDiff;
294 if ($dayDiff < 0) {
295 --$ageInMonths;
298 if ($format == 1 || ($format == 0 && $ageInMonths >= 24)) {
299 $age = $yearDiff;
300 if ($monthDiff < 0 || ($monthDiff == 0 && $dayDiff < 0)) {
301 --$age;
303 } else {
304 $age = $ageInMonths;
305 if ($format == 0) {
306 $age .= ' ' . $ageInMonths == 1 ? xl('month') : xl('months');
311 return $age;