Improvements for statements
[openemr.git] / library / formatting.inc.php
blob33dc3b115cb4e9618e60d759ad6bcb984647ee95
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') {
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
25 $date = substr($date, 5, 2) . '/' . substr($date, 8, 2) . '/' . substr($date, 0, 4);
26 else if ($GLOBALS['date_display_format'] == 2) // dd/mm/yyyy
27 $date = substr($date, 8, 2) . '/' . substr($date, 5, 2) . '/' . substr($date, 0, 4);
29 return $date;
32 // 0 - Time format 24 hr
33 // 1 - Time format 12 hr
34 function oeFormatTime( $time, $format = "" )
36 $formatted = $time;
37 if ( $format == "" ) {
38 $format = $GLOBALS['time_display_format'];
41 if ( $format == 0 ) {
42 $formatted = date( "H:i", strtotime( $time ) );
43 } else if ( $format == 1 ) {
44 $formatted = date( "g:i a", strtotime( $time ) );
47 return $formatted;
50 // Format short date from time.
51 function oeFormatSDFT($time) {
52 return oeFormatShortDate(date('Y-m-d', $time));
55 // Format the body of a patient note.
56 function oeFormatPatientNote($note) {
57 $i = 0;
58 while ($i !== false) {
59 if (preg_match('/^\d\d\d\d-\d\d-\d\d/', substr($note, $i))) {
60 $note = substr($note, 0, $i) . oeFormatShortDate(substr($note, $i, 10)) . substr($note, $i + 10);
62 $i = strpos("\n", $note, $i);
63 if ($i !== false) ++$i;
65 return $note;
68 function oeFormatClientID($id) {
70 // TBD
72 return $id;
74 //----------------------------------------------------
75 function DateFormatRead()
76 {//For the 3 supported date format,the javascript code also should be twicked to display the date as per it.
77 //Output of this function is given to 'ifFormat' parameter of the 'Calendar.setup'.
78 //This will show the date as per the global settings.
79 if($GLOBALS['date_display_format']==0)
81 return "%Y-%m-%d";
83 else if($GLOBALS['date_display_format']==1)
85 return "%m/%d/%Y";
87 else if($GLOBALS['date_display_format']==2)
89 return "%d/%m/%Y";
92 function DateToYYYYMMDD($DateValue)
93 {//With the help of function DateFormatRead() now the user can enter date is any of the 3 formats depending upon the global setting.
94 //But in database the date can be stored only in the yyyy-mm-dd format.
95 //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.
96 if(trim($DateValue)=='')
98 return '';
101 if($GLOBALS['date_display_format']==0)
103 return $DateValue;
105 else if($GLOBALS['date_display_format']==1 || $GLOBALS['date_display_format']==2)
107 $DateValueArray=explode('/',$DateValue);
108 if($GLOBALS['date_display_format']==1)
110 return $DateValueArray[2].'-'.$DateValueArray[0].'-'.$DateValueArray[1];
112 if($GLOBALS['date_display_format']==2)
114 return $DateValueArray[2].'-'.$DateValueArray[1].'-'.$DateValueArray[0];
119 // Returns age in a desired format:
120 // 0 = "xx month(s)" if < 2 years, else years
121 // 1 = Years : just a number
122 // 2 = Months : just a number
123 // 3 = Gestational: "xx week(s) y day(s)"
124 // $dobYMD is YYYYMMDD or YYYY-MM-DD
125 // $nowYMD is same format but optional
127 function oeFormatAge($dobYMD, $nowYMD='', $format=0) {
128 // Strip any dashes from the dates.
129 $dobYMD = preg_replace('/-/', '', $dobYMD);
130 $nowYMD = preg_replace('/-/', '', $nowYMD);
131 $dobDay = substr($dobYMD,6,2);
132 $dobMonth = substr($dobYMD,4,2);
133 $dobYear = substr($dobYMD,0,4);
135 if ($nowYMD) {
136 $nowDay = substr($nowYMD,6,2);
137 $nowMonth = substr($nowYMD,4,2);
138 $nowYear = substr($nowYMD,0,4);
140 else {
141 $nowDay = date("d");
142 $nowMonth = date("m");
143 $nowYear = date("Y");
146 if ($format == 3) {
147 // Gestational age as weeks and days.
148 $secs = mktime(0, 0, 0, $nowMonth, $nowDay, $nowYear) -
149 mktime(0, 0, 0, $dobMonth, $dobDay, $dobYear);
150 $days = intval($secs / (24 * 60 * 60));
151 $weeks = intval($days / 7);
152 $days = $days % 7;
153 $age = "$weeks " . ($weeks == 1 ? xl('week') : xl('weeks')) .
154 " $days " . ($days == 1 ? xl('day' ) : xl('days' ));
156 else {
157 // Years or months.
158 $dayDiff = $nowDay - $dobDay;
159 $monthDiff = $nowMonth - $dobMonth;
160 $yearDiff = $nowYear - $dobYear;
161 $ageInMonths = $yearDiff * 12 + $monthDiff;
162 if ($dayDiff < 0) --$ageInMonths;
163 if ($format == 1 || ($format == 0 && $ageInMonths >= 24)) {
164 $age = $yearDiff;
165 if ($monthDiff < 0 || ($monthDiff == 0 && $dayDiff < 0)) --$age;
167 else {
168 $age = $ageInMonths;
169 if ($format == 0) {
170 $age .= ' ' . $ageInMonths == 1 ? xl('month') : xl('months');
175 return $age;