Merge pull request #1761 from sjpadgett/PP2-appointment
[openemr.git] / library / formatting.inc.php
blobbf83abe53efe91accc159a983a8a9357fab81dfa
1 <?php
2 /**
3 * Formatting library.
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Rod Roark <rod@sunsetsystems.com>
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2010-2014 Rod Roark <rod@sunsetsystems.com>
10 * @copyright Copyright (c) 2017-2018 Brady Miller <brady.g.miller@gmail.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
14 function oeFormatMoney($amount, $symbol = false)
16 $s = number_format(
17 floatval($amount),
18 $GLOBALS['currency_decimals'],
19 $GLOBALS['currency_dec_point'],
20 $GLOBALS['currency_thousands_sep']
22 // If the currency symbol exists and is requested, prepend it.
23 if ($symbol && !empty($GLOBALS['gbl_currency_symbol'])) {
24 $s = $GLOBALS['gbl_currency_symbol'] . " $s";
27 return $s;
30 function oeFormatShortDate($date = 'today', $showYear = true)
32 if ($date === 'today') {
33 $date = date('Y-m-d');
36 if (strlen($date) >= 10) {
37 // assume input is yyyy-mm-dd
38 if ($GLOBALS['date_display_format'] == 1) { // mm/dd/yyyy, note year is added below
39 $newDate = substr($date, 5, 2) . '/' . substr($date, 8, 2);
40 } else if ($GLOBALS['date_display_format'] == 2) { // dd/mm/yyyy, note year is added below
41 $newDate = substr($date, 8, 2) . '/' . substr($date, 5, 2);
44 // process the year (add for formats 1 and 2; remove for format 0)
45 if ($GLOBALS['date_display_format'] == 1 || $GLOBALS['date_display_format'] == 2) {
46 if ($showYear) {
47 $newDate .= '/' . substr($date, 0, 4);
49 } else if (!$showYear) { // $GLOBALS['date_display_format'] == 0
50 // need to remove the year
51 $newDate = substr($date, 5, 2) . '-' . substr($date, 8, 2);
52 } else { // $GLOBALS['date_display_format'] == 0
53 // keep the year (so will simply be the original $date)
54 $newDate = $date;
57 return $newDate;
60 // this is case if the $date does not have 10 characters
61 return $date;
64 // 0 - Time format 24 hr
65 // 1 - Time format 12 hr
66 function oeFormatTime($time, $format = "global", $seconds = false)
68 if (empty($time)) {
69 return "";
72 $formatted = $time;
74 if ($format === "global") {
75 $format = $GLOBALS['time_display_format'];
79 if ($format == 1) {
80 if ($seconds) {
81 $formatted = date("g:i:s a", strtotime($time));
82 } else {
83 $formatted = date("g:i a", strtotime($time));
85 } else { // ($format == 0)
86 if ($seconds) {
87 $formatted = date("H:i:s", strtotime($time));
88 } else {
89 $formatted = date("H:i", strtotime($time));
93 return $formatted;
96 /**
97 * Returns the complete formatted datetime string according the global date and time format
98 * @param $datetime
99 * @return string
101 function oeFormatDateTime($datetime, $formatTime = "global", $seconds = false)
103 return oeFormatShortDate(substr($datetime, 0, 10)) . " " . oeFormatTime(substr($datetime, 11), $formatTime, $seconds);
107 * Returns the complete formatted datetime string according the global date and time format
108 * @param $timestamp
109 * @return string
111 function oeTimestampFormatDateTime($timestamp)
113 if (!$timestamp) {
114 $timestamp = strtotime(date('Y-m-d H:i'));
117 if ($GLOBALS['time_display_format'] == 0) {
118 $timeFormat = 'H:i';
119 } else { // $GLOBALS['time_display_format'] == 1
120 $timeFormat = 'g:i a';
123 if ($GLOBALS['date_display_format'] == 1) { // mm/dd/yyyy
124 $newDate = date('m/d/Y ' . $timeFormat, $timestamp);
125 } else if ($GLOBALS['date_display_format'] == 2) { // dd/mm/yyyy
126 $newDate = date('d/m/Y ' . $timeFormat, $timestamp);
127 } else { // yyyy-mm-dd
128 $newDate = date('Y-m-d ' . $timeFormat, $timestamp);
131 return $newDate;
134 // Format short date from time.
135 function oeFormatSDFT($time)
137 return oeFormatShortDate(date('Y-m-d', $time));
140 // Format the body of a patient note.
141 function oeFormatPatientNote($note)
143 $i = 0;
144 while ($i !== false) {
145 if (preg_match('/^\d\d\d\d-\d\d-\d\d/', substr($note, $i))) {
146 $note = substr($note, 0, $i) . oeFormatShortDate(substr($note, $i, 10)) . substr($note, $i + 10);
149 $i = strpos("\n", $note, $i);
150 if ($i !== false) {
151 ++$i;
155 return $note;
158 function oeFormatClientID($id)
161 // TBD
163 return $id;
165 //----------------------------------------------------
166 function DateFormatRead($mode = 'legacy')
168 //For the 3 supported date format,the javascript code also should be twicked to display the date as per it.
169 //Output of this function is given to 'ifFormat' parameter of the 'Calendar.setup'.
170 //This will show the date as per the global settings.
171 if ($GLOBALS['date_display_format']==0) {
172 if ($mode == 'legacy') {
173 return "%Y-%m-%d";
174 } elseif ($mode == 'validateJS') {
175 return "YYYY-MM-DD";
176 } else { //$mode=='jquery-datetimepicker'
177 return "Y-m-d";
179 } else if ($GLOBALS['date_display_format']==1) {
180 if ($mode == 'legacy') {
181 return "%m/%d/%Y";
182 } elseif ($mode == 'validateJS') {
183 return "MM/DD/YYYY";
184 } else { //$mode=='jquery-datetimepicker'
185 return "m/d/Y";
187 } else if ($GLOBALS['date_display_format']==2) {
188 if ($mode == 'legacy') {
189 return "%d/%m/%Y";
190 } elseif ($mode == 'validateJS') {
191 return "DD/MM/YYYY";
192 } else { //$mode=='jquery-datetimepicker'
193 return "d/m/Y";
198 function DateToYYYYMMDD($DateValue)
200 //With the help of function DateFormatRead() now the user can enter date is any of the 3 formats depending upon the global setting.
201 //But in database the date can be stored only in the yyyy-mm-dd format.
202 //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.
203 if (trim($DateValue)=='') {
204 return '';
207 if ($GLOBALS['date_display_format']==0) {
208 return $DateValue;
209 } else if ($GLOBALS['date_display_format']==1 || $GLOBALS['date_display_format']==2) {
210 $DateValueArray=explode('/', $DateValue);
211 if ($GLOBALS['date_display_format']==1) {
212 return $DateValueArray[2].'-'.$DateValueArray[0].'-'.$DateValueArray[1];
215 if ($GLOBALS['date_display_format']==2) {
216 return $DateValueArray[2].'-'.$DateValueArray[1].'-'.$DateValueArray[0];
221 function TimeToHHMMSS($TimeValue)
223 //For now, just return the $TimeValue, since input fields are not formatting time.
224 // This can be upgraded if decided to format input time fields.
226 if (trim($TimeValue)=='') {
227 return '';
230 return $TimeValue;
234 function DateTimeToYYYYMMDDHHMMSS($DateTimeValue)
236 //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.
238 // First deal with the date
239 $fixed_date = DateToYYYYMMDD(substr($DateTimeValue, 0, 10));
241 // Then deal with the time
242 $fixed_time = TimeToHHMMSS(substr($DateTimeValue, 11));
244 if (empty($fixed_date) && empty($fixed_time)) {
245 return "";
246 } else {
247 return $fixed_date . " " . $fixed_time;
251 // Returns age in a desired format:
252 // 0 = "xx month(s)" if < 2 years, else years
253 // 1 = Years : just a number
254 // 2 = Months : just a number
255 // 3 = Gestational: "xx week(s) y day(s)"
256 // $dobYMD is YYYYMMDD or YYYY-MM-DD
257 // $nowYMD is same format but optional
259 function oeFormatAge($dobYMD, $nowYMD = '', $format = 0)
261 // Strip any dashes from the dates.
262 $dobYMD = preg_replace('/-/', '', $dobYMD);
263 $nowYMD = preg_replace('/-/', '', $nowYMD);
264 $dobDay = substr($dobYMD, 6, 2);
265 $dobMonth = substr($dobYMD, 4, 2);
266 $dobYear = substr($dobYMD, 0, 4);
268 if ($nowYMD) {
269 $nowDay = substr($nowYMD, 6, 2);
270 $nowMonth = substr($nowYMD, 4, 2);
271 $nowYear = substr($nowYMD, 0, 4);
272 } else {
273 $nowDay = date("d");
274 $nowMonth = date("m");
275 $nowYear = date("Y");
278 if ($format == 3) {
279 // Gestational age as weeks and days.
280 $secs = mktime(0, 0, 0, $nowMonth, $nowDay, $nowYear) -
281 mktime(0, 0, 0, $dobMonth, $dobDay, $dobYear);
282 $days = intval($secs / (24 * 60 * 60));
283 $weeks = intval($days / 7);
284 $days = $days % 7;
285 $age = "$weeks " . ($weeks == 1 ? xl('week') : xl('weeks')) .
286 " $days " . ($days == 1 ? xl('day') : xl('days'));
287 } else {
288 // Years or months.
289 $dayDiff = $nowDay - $dobDay;
290 $monthDiff = $nowMonth - $dobMonth;
291 $yearDiff = $nowYear - $dobYear;
292 $ageInMonths = $yearDiff * 12 + $monthDiff;
293 if ($dayDiff < 0) {
294 --$ageInMonths;
297 if ($format == 1 || ($format == 0 && $ageInMonths >= 24)) {
298 $age = $yearDiff;
299 if ($monthDiff < 0 || ($monthDiff == 0 && $dayDiff < 0)) {
300 --$age;
302 } else {
303 $age = $ageInMonths;
304 if ($format == 0) {
305 $age .= ' ' . $ageInMonths == 1 ? xl('month') : xl('months');
310 return $age;