Practices gui - bootstrap light, no pure php in smarty, html escaping in smarty ...
[openemr.git] / library / formatting.inc.php
blobdada688f3fbaa5c3ac2f46c694ab0ae62a91a581
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) {
10 $s = number_format($amount,
11 $GLOBALS['currency_decimals'],
12 $GLOBALS['currency_dec_point'],
13 $GLOBALS['currency_thousands_sep']);
14 // If the currency symbol exists and is requested, prepend it.
15 if ($symbol && !empty($GLOBALS['gbl_currency_symbol']))
16 $s = $GLOBALS['gbl_currency_symbol'] . " $s";
17 return $s;
20 function oeFormatShortDate($date='today', $showYear = true) {
21 if ($date === 'today') $date = date('Y-m-d');
22 if (strlen($date) == 10) {
23 // assume input is yyyy-mm-dd
24 if ($GLOBALS['date_display_format'] == 1) // mm/dd/yyyy, note year is added below
25 $newDate = substr($date, 5, 2) . '/' . substr($date, 8, 2);
26 else if ($GLOBALS['date_display_format'] == 2) // dd/mm/yyyy, note year is added below
27 $newDate = substr($date, 8, 2) . '/' . substr($date, 5, 2);
29 // process the year (add for formats 1 and 2; remove for format 0)
30 if ($GLOBALS['date_display_format'] == 1 || $GLOBALS['date_display_format'] == 2) {
31 if ($showYear) {
32 $newDate .= '/' . substr($date, 0, 4);
35 else if (!$showYear) { // $GLOBALS['date_display_format'] == 0
36 // need to remove the year
37 $newDate = substr($date, 5, 2) . '-' . substr($date, 8, 2);
39 else { // $GLOBALS['date_display_format'] == 0
40 // keep the year (so will simply be the original $date)
41 $newDate = $date;
44 return $newDate;
47 // this is case if the $date does not have 10 characters
48 return $date;
51 // 0 - Time format 24 hr
52 // 1 - Time format 12 hr
53 function oeFormatTime( $time, $format = "" )
55 $formatted = $time;
56 if ( $format == "" ) {
57 $format = $GLOBALS['time_display_format'];
60 if ( $format == 0 ) {
61 $formatted = date( "H:i", strtotime( $time ) );
62 } else if ( $format == 1 ) {
63 $formatted = date( "g:i a", strtotime( $time ) );
66 return $formatted;
69 // Format short date from time.
70 function oeFormatSDFT($time) {
71 return oeFormatShortDate(date('Y-m-d', $time));
74 // Format the body of a patient note.
75 function oeFormatPatientNote($note) {
76 $i = 0;
77 while ($i !== false) {
78 if (preg_match('/^\d\d\d\d-\d\d-\d\d/', substr($note, $i))) {
79 $note = substr($note, 0, $i) . oeFormatShortDate(substr($note, $i, 10)) . substr($note, $i + 10);
81 $i = strpos("\n", $note, $i);
82 if ($i !== false) ++$i;
84 return $note;
87 function oeFormatClientID($id) {
89 // TBD
91 return $id;
93 //----------------------------------------------------
94 function DateFormatRead($mode='legacy') {
95 //For the 3 supported date format,the javascript code also should be twicked to display the date as per it.
96 //Output of this function is given to 'ifFormat' parameter of the 'Calendar.setup'.
97 //This will show the date as per the global settings.
98 if($GLOBALS['date_display_format']==0) {
99 if ($mode == 'legacy') {
100 return "%Y-%m-%d";
102 else { //$mode=='jquery-datetimepicker'
103 return "Y-m-d";
106 else if($GLOBALS['date_display_format']==1) {
107 if ($mode == 'legacy') {
108 return "%m/%d/%Y";
110 else { //$mode=='jquery-datetimepicker'
111 return "m/d/Y";
114 else if($GLOBALS['date_display_format']==2) {
115 if ($mode == 'legacy') {
116 return "%d/%m/%Y";
118 else { //$mode=='jquery-datetimepicker'
119 return "d/m/Y";
124 function DateToYYYYMMDD($DateValue)
125 {//With the help of function DateFormatRead() now the user can enter date is any of the 3 formats depending upon the global setting.
126 //But in database the date can be stored only in the yyyy-mm-dd format.
127 //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.
128 if(trim($DateValue)=='')
130 return '';
133 if($GLOBALS['date_display_format']==0)
135 return $DateValue;
137 else if($GLOBALS['date_display_format']==1 || $GLOBALS['date_display_format']==2)
139 $DateValueArray=explode('/',$DateValue);
140 if($GLOBALS['date_display_format']==1)
142 return $DateValueArray[2].'-'.$DateValueArray[0].'-'.$DateValueArray[1];
144 if($GLOBALS['date_display_format']==2)
146 return $DateValueArray[2].'-'.$DateValueArray[1].'-'.$DateValueArray[0];
151 // Returns age in a desired format:
152 // 0 = "xx month(s)" if < 2 years, else years
153 // 1 = Years : just a number
154 // 2 = Months : just a number
155 // 3 = Gestational: "xx week(s) y day(s)"
156 // $dobYMD is YYYYMMDD or YYYY-MM-DD
157 // $nowYMD is same format but optional
159 function oeFormatAge($dobYMD, $nowYMD='', $format=0) {
160 // Strip any dashes from the dates.
161 $dobYMD = preg_replace('/-/', '', $dobYMD);
162 $nowYMD = preg_replace('/-/', '', $nowYMD);
163 $dobDay = substr($dobYMD,6,2);
164 $dobMonth = substr($dobYMD,4,2);
165 $dobYear = substr($dobYMD,0,4);
167 if ($nowYMD) {
168 $nowDay = substr($nowYMD,6,2);
169 $nowMonth = substr($nowYMD,4,2);
170 $nowYear = substr($nowYMD,0,4);
172 else {
173 $nowDay = date("d");
174 $nowMonth = date("m");
175 $nowYear = date("Y");
178 if ($format == 3) {
179 // Gestational age as weeks and days.
180 $secs = mktime(0, 0, 0, $nowMonth, $nowDay, $nowYear) -
181 mktime(0, 0, 0, $dobMonth, $dobDay, $dobYear);
182 $days = intval($secs / (24 * 60 * 60));
183 $weeks = intval($days / 7);
184 $days = $days % 7;
185 $age = "$weeks " . ($weeks == 1 ? xl('week') : xl('weeks')) .
186 " $days " . ($days == 1 ? xl('day' ) : xl('days' ));
188 else {
189 // Years or months.
190 $dayDiff = $nowDay - $dobDay;
191 $monthDiff = $nowMonth - $dobMonth;
192 $yearDiff = $nowYear - $dobYear;
193 $ageInMonths = $yearDiff * 12 + $monthDiff;
194 if ($dayDiff < 0) --$ageInMonths;
195 if ($format == 1 || ($format == 0 && $ageInMonths >= 24)) {
196 $age = $yearDiff;
197 if ($monthDiff < 0 || ($monthDiff == 0 && $dayDiff < 0)) --$age;
199 else {
200 $age = $ageInMonths;
201 if ($format == 0) {
202 $age .= ' ' . $ageInMonths == 1 ? xl('month') : xl('months');
207 return $age;