Added access controls for encounter categories
[openemr.git] / library / formatting.inc.php
blob0b509032574340c65a2d56061e3b56d1ed3e3d07
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($amount,
12 $GLOBALS['currency_decimals'],
13 $GLOBALS['currency_dec_point'],
14 $GLOBALS['currency_thousands_sep']);
15 // If the currency symbol exists and is requested, prepend it.
16 if ($symbol && !empty($GLOBALS['gbl_currency_symbol']))
17 $s = $GLOBALS['gbl_currency_symbol'] . " $s";
18 return $s;
21 function oeFormatShortDate($date='today', $showYear = true)
23 if ($date === 'today') $date = date('Y-m-d');
24 if (strlen($date) == 10) {
25 // assume input is yyyy-mm-dd
26 if ($GLOBALS['date_display_format'] == 1) // mm/dd/yyyy, note year is added below
27 $newDate = substr($date, 5, 2) . '/' . substr($date, 8, 2);
28 else if ($GLOBALS['date_display_format'] == 2) // dd/mm/yyyy, note year is added below
29 $newDate = substr($date, 8, 2) . '/' . substr($date, 5, 2);
31 // process the year (add for formats 1 and 2; remove for format 0)
32 if ($GLOBALS['date_display_format'] == 1 || $GLOBALS['date_display_format'] == 2) {
33 if ($showYear) {
34 $newDate .= '/' . substr($date, 0, 4);
37 else if (!$showYear) { // $GLOBALS['date_display_format'] == 0
38 // need to remove the year
39 $newDate = substr($date, 5, 2) . '-' . substr($date, 8, 2);
41 else { // $GLOBALS['date_display_format'] == 0
42 // keep the year (so will simply be the original $date)
43 $newDate = $date;
46 return $newDate;
49 // this is case if the $date does not have 10 characters
50 return $date;
53 // 0 - Time format 24 hr
54 // 1 - Time format 12 hr
55 function oeFormatTime( $time, $format = "" )
57 $formatted = $time;
58 if ( $format == "" ) {
59 $format = $GLOBALS['time_display_format'];
62 if ( $format == 0 ) {
63 $formatted = date( "H:i", strtotime( $time ) );
64 } else if ( $format == 1 ) {
65 $formatted = date( "g:i a", strtotime( $time ) );
68 return $formatted;
71 // Format short date from time.
72 function oeFormatSDFT($time)
74 return oeFormatShortDate(date('Y-m-d', $time));
77 // Format the body of a patient note.
78 function oeFormatPatientNote($note)
80 $i = 0;
81 while ($i !== false) {
82 if (preg_match('/^\d\d\d\d-\d\d-\d\d/', substr($note, $i))) {
83 $note = substr($note, 0, $i) . oeFormatShortDate(substr($note, $i, 10)) . substr($note, $i + 10);
85 $i = strpos("\n", $note, $i);
86 if ($i !== false) ++$i;
88 return $note;
91 function oeFormatClientID($id)
94 // TBD
96 return $id;
98 //----------------------------------------------------
99 function DateFormatRead($mode='legacy')
101 //For the 3 supported date format,the javascript code also should be twicked to display the date as per it.
102 //Output of this function is given to 'ifFormat' parameter of the 'Calendar.setup'.
103 //This will show the date as per the global settings.
104 if($GLOBALS['date_display_format']==0) {
105 if ($mode == 'legacy') {
106 return "%Y-%m-%d";
108 else { //$mode=='jquery-datetimepicker'
109 return "Y-m-d";
112 else if($GLOBALS['date_display_format']==1) {
113 if ($mode == 'legacy') {
114 return "%m/%d/%Y";
116 else { //$mode=='jquery-datetimepicker'
117 return "m/d/Y";
120 else if($GLOBALS['date_display_format']==2) {
121 if ($mode == 'legacy') {
122 return "%d/%m/%Y";
124 else { //$mode=='jquery-datetimepicker'
125 return "d/m/Y";
130 function DateToYYYYMMDD($DateValue)
132 //With the help of function DateFormatRead() now the user can enter date is any of the 3 formats depending upon the global setting.
133 //But in database the date can be stored only in the yyyy-mm-dd format.
134 //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.
135 if(trim($DateValue)=='')
137 return '';
140 if($GLOBALS['date_display_format']==0)
142 return $DateValue;
144 else if($GLOBALS['date_display_format']==1 || $GLOBALS['date_display_format']==2)
146 $DateValueArray=explode('/',$DateValue);
147 if($GLOBALS['date_display_format']==1)
149 return $DateValueArray[2].'-'.$DateValueArray[0].'-'.$DateValueArray[1];
151 if($GLOBALS['date_display_format']==2)
153 return $DateValueArray[2].'-'.$DateValueArray[1].'-'.$DateValueArray[0];
158 // Returns age in a desired format:
159 // 0 = "xx month(s)" if < 2 years, else years
160 // 1 = Years : just a number
161 // 2 = Months : just a number
162 // 3 = Gestational: "xx week(s) y day(s)"
163 // $dobYMD is YYYYMMDD or YYYY-MM-DD
164 // $nowYMD is same format but optional
166 function oeFormatAge($dobYMD, $nowYMD='', $format=0)
168 // Strip any dashes from the dates.
169 $dobYMD = preg_replace('/-/', '', $dobYMD);
170 $nowYMD = preg_replace('/-/', '', $nowYMD);
171 $dobDay = substr($dobYMD,6,2);
172 $dobMonth = substr($dobYMD,4,2);
173 $dobYear = substr($dobYMD,0,4);
175 if ($nowYMD) {
176 $nowDay = substr($nowYMD,6,2);
177 $nowMonth = substr($nowYMD,4,2);
178 $nowYear = substr($nowYMD,0,4);
180 else {
181 $nowDay = date("d");
182 $nowMonth = date("m");
183 $nowYear = date("Y");
186 if ($format == 3) {
187 // Gestational age as weeks and days.
188 $secs = mktime(0, 0, 0, $nowMonth, $nowDay, $nowYear) -
189 mktime(0, 0, 0, $dobMonth, $dobDay, $dobYear);
190 $days = intval($secs / (24 * 60 * 60));
191 $weeks = intval($days / 7);
192 $days = $days % 7;
193 $age = "$weeks " . ($weeks == 1 ? xl('week') : xl('weeks')) .
194 " $days " . ($days == 1 ? xl('day' ) : xl('days' ));
196 else {
197 // Years or months.
198 $dayDiff = $nowDay - $dobDay;
199 $monthDiff = $nowMonth - $dobMonth;
200 $yearDiff = $nowYear - $dobYear;
201 $ageInMonths = $yearDiff * 12 + $monthDiff;
202 if ($dayDiff < 0) --$ageInMonths;
203 if ($format == 1 || ($format == 0 && $ageInMonths >= 24)) {
204 $age = $yearDiff;
205 if ($monthDiff < 0 || ($monthDiff == 0 && $dayDiff < 0)) --$age;
207 else {
208 $age = $ageInMonths;
209 if ($format == 0) {
210 $age .= ' ' . $ageInMonths == 1 ? xl('month') : xl('months');
215 return $age;