Updated gui for user facility settings (#1327)
[openemr.git] / library / formatting.inc.php
blobdc9b22a7b702782a023c6e9f4ba334af0dabfd07
1 <?php
2 // Copyright (C) 2010-2014 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 function oeFormatMoney($amount, $symbol = false)
11 $s = number_format(
12 floatval($amount),
13 $GLOBALS['currency_decimals'],
14 $GLOBALS['currency_dec_point'],
15 $GLOBALS['currency_thousands_sep']
17 // If the currency symbol exists and is requested, prepend it.
18 if ($symbol && !empty($GLOBALS['gbl_currency_symbol'])) {
19 $s = $GLOBALS['gbl_currency_symbol'] . " $s";
22 return $s;
25 function oeFormatShortDate($date = 'today', $showYear = true)
27 if ($date === 'today') {
28 $date = date('Y-m-d');
31 if (strlen($date) >= 10) {
32 // assume input is yyyy-mm-dd
33 if ($GLOBALS['date_display_format'] == 1) { // mm/dd/yyyy, note year is added below
34 $newDate = substr($date, 5, 2) . '/' . substr($date, 8, 2);
35 } else if ($GLOBALS['date_display_format'] == 2) { // dd/mm/yyyy, note year is added below
36 $newDate = substr($date, 8, 2) . '/' . substr($date, 5, 2);
39 // process the year (add for formats 1 and 2; remove for format 0)
40 if ($GLOBALS['date_display_format'] == 1 || $GLOBALS['date_display_format'] == 2) {
41 if ($showYear) {
42 $newDate .= '/' . substr($date, 0, 4);
44 } else if (!$showYear) { // $GLOBALS['date_display_format'] == 0
45 // need to remove the year
46 $newDate = substr($date, 5, 2) . '-' . substr($date, 8, 2);
47 } else { // $GLOBALS['date_display_format'] == 0
48 // keep the year (so will simply be the original $date)
49 $newDate = $date;
52 return $newDate;
55 // this is case if the $date does not have 10 characters
56 return $date;
59 // 0 - Time format 24 hr
60 // 1 - Time format 12 hr
61 function oeFormatTime($time, $format = "")
63 $formatted = $time;
64 if ($format == "") {
65 $format = $GLOBALS['time_display_format'];
68 if ($format == 0) {
69 $formatted = date("H:i", strtotime($time));
70 } else if ($format == 1) {
71 $formatted = date("g:i a", strtotime($time));
74 return $formatted;
77 /**
78 * Returns the complete formatted datetime string according the global date and time format
79 * @param $datetime
80 * @return string
82 function oeFormatDateTime($datetime)
84 echo oeFormatShortDate(substr($datetime, 0, 10)) . " " . oeFormatTime(substr($datetime, 10));
87 /**
88 * Returns the complete formatted datetime string according the global date and time format
89 * @param $timestamp
90 * @return string
92 function oeTimestampFormatDateTime($timestamp)
94 if (!$timestamp) {
95 $timestamp = strtotime(date('Y-m-d H:i'));
98 if ($GLOBALS['time_display_format'] == 0) {
99 $timeFormat = 'H:i';
100 } else { // $GLOBALS['time_display_format'] == 1
101 $timeFormat = 'g:i a';
104 if ($GLOBALS['date_display_format'] == 1) { // mm/dd/yyyy
105 $newDate = date('m/d/Y ' . $timeFormat, $timestamp);
106 } else if ($GLOBALS['date_display_format'] == 2) { // dd/mm/yyyy
107 $newDate = date('d/m/Y ' . $timeFormat, $timestamp);
108 } else { // yyyy-mm-dd
109 $newDate = date('Y-m-d ' . $timeFormat, $timestamp);
112 return $newDate;
115 // Format short date from time.
116 function oeFormatSDFT($time)
118 return oeFormatShortDate(date('Y-m-d', $time));
121 // Format the body of a patient note.
122 function oeFormatPatientNote($note)
124 $i = 0;
125 while ($i !== false) {
126 if (preg_match('/^\d\d\d\d-\d\d-\d\d/', substr($note, $i))) {
127 $note = substr($note, 0, $i) . oeFormatShortDate(substr($note, $i, 10)) . substr($note, $i + 10);
130 $i = strpos("\n", $note, $i);
131 if ($i !== false) {
132 ++$i;
136 return $note;
139 function oeFormatClientID($id)
142 // TBD
144 return $id;
146 //----------------------------------------------------
147 function DateFormatRead($mode = 'legacy')
149 //For the 3 supported date format,the javascript code also should be twicked to display the date as per it.
150 //Output of this function is given to 'ifFormat' parameter of the 'Calendar.setup'.
151 //This will show the date as per the global settings.
152 if ($GLOBALS['date_display_format']==0) {
153 if ($mode == 'legacy') {
154 return "%Y-%m-%d";
155 } else { //$mode=='jquery-datetimepicker'
156 return "Y-m-d";
158 } else if ($GLOBALS['date_display_format']==1) {
159 if ($mode == 'legacy') {
160 return "%m/%d/%Y";
161 } else { //$mode=='jquery-datetimepicker'
162 return "m/d/Y";
164 } else if ($GLOBALS['date_display_format']==2) {
165 if ($mode == 'legacy') {
166 return "%d/%m/%Y";
167 } else { //$mode=='jquery-datetimepicker'
168 return "d/m/Y";
173 function DateToYYYYMMDD($DateValue)
175 //With the help of function DateFormatRead() now the user can enter date is any of the 3 formats depending upon the global setting.
176 //But in database the date can be stored only in the yyyy-mm-dd format.
177 //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.
178 if (trim($DateValue)=='') {
179 return '';
182 if ($GLOBALS['date_display_format']==0) {
183 return $DateValue;
184 } else if ($GLOBALS['date_display_format']==1 || $GLOBALS['date_display_format']==2) {
185 $DateValueArray=explode('/', $DateValue);
186 if ($GLOBALS['date_display_format']==1) {
187 return $DateValueArray[2].'-'.$DateValueArray[0].'-'.$DateValueArray[1];
190 if ($GLOBALS['date_display_format']==2) {
191 return $DateValueArray[2].'-'.$DateValueArray[1].'-'.$DateValueArray[0];
196 // Returns age in a desired format:
197 // 0 = "xx month(s)" if < 2 years, else years
198 // 1 = Years : just a number
199 // 2 = Months : just a number
200 // 3 = Gestational: "xx week(s) y day(s)"
201 // $dobYMD is YYYYMMDD or YYYY-MM-DD
202 // $nowYMD is same format but optional
204 function oeFormatAge($dobYMD, $nowYMD = '', $format = 0)
206 // Strip any dashes from the dates.
207 $dobYMD = preg_replace('/-/', '', $dobYMD);
208 $nowYMD = preg_replace('/-/', '', $nowYMD);
209 $dobDay = substr($dobYMD, 6, 2);
210 $dobMonth = substr($dobYMD, 4, 2);
211 $dobYear = substr($dobYMD, 0, 4);
213 if ($nowYMD) {
214 $nowDay = substr($nowYMD, 6, 2);
215 $nowMonth = substr($nowYMD, 4, 2);
216 $nowYear = substr($nowYMD, 0, 4);
217 } else {
218 $nowDay = date("d");
219 $nowMonth = date("m");
220 $nowYear = date("Y");
223 if ($format == 3) {
224 // Gestational age as weeks and days.
225 $secs = mktime(0, 0, 0, $nowMonth, $nowDay, $nowYear) -
226 mktime(0, 0, 0, $dobMonth, $dobDay, $dobYear);
227 $days = intval($secs / (24 * 60 * 60));
228 $weeks = intval($days / 7);
229 $days = $days % 7;
230 $age = "$weeks " . ($weeks == 1 ? xl('week') : xl('weeks')) .
231 " $days " . ($days == 1 ? xl('day') : xl('days'));
232 } else {
233 // Years or months.
234 $dayDiff = $nowDay - $dobDay;
235 $monthDiff = $nowMonth - $dobMonth;
236 $yearDiff = $nowYear - $dobYear;
237 $ageInMonths = $yearDiff * 12 + $monthDiff;
238 if ($dayDiff < 0) {
239 --$ageInMonths;
242 if ($format == 1 || ($format == 0 && $ageInMonths >= 24)) {
243 $age = $yearDiff;
244 if ($monthDiff < 0 || ($monthDiff == 0 && $dayDiff < 0)) {
245 --$age;
247 } else {
248 $age = $ageInMonths;
249 if ($format == 0) {
250 $age .= ' ' . $ageInMonths == 1 ? xl('month') : xl('months');
255 return $age;