forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatting.inc.php
196 lines (178 loc) · 5.88 KB
/
formatting.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
// Copyright (C) 2010-2014 Rod Roark <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
function oeFormatMoney($amount, $symbol=false) {
$s = number_format($amount,
$GLOBALS['currency_decimals'],
$GLOBALS['currency_dec_point'],
$GLOBALS['currency_thousands_sep']);
// If the currency symbol exists and is requested, prepend it.
if ($symbol && !empty($GLOBALS['gbl_currency_symbol']))
$s = $GLOBALS['gbl_currency_symbol'] . " $s";
return $s;
}
function oeFormatShortDate($date='today', $showYear = true) {
if ($date === 'today') $date = date('Y-m-d');
if (strlen($date) == 10) {
// assume input is yyyy-mm-dd
if ($GLOBALS['date_display_format'] == 1) // mm/dd/yyyy, note year is added below
$newDate = substr($date, 5, 2) . '/' . substr($date, 8, 2);
else if ($GLOBALS['date_display_format'] == 2) // dd/mm/yyyy, note year is added below
$newDate = substr($date, 8, 2) . '/' . substr($date, 5, 2);
// process the year (add for formats 1 and 2; remove for format 0)
if ($GLOBALS['date_display_format'] == 1 || $GLOBALS['date_display_format'] == 2) {
if ($showYear) {
$newDate .= '/' . substr($date, 0, 4);
}
}
else if (!$showYear) { // $GLOBALS['date_display_format'] == 0
// need to remove the year
$newDate = substr($date, 5, 2) . '-' . substr($date, 8, 2);
}
else { // $GLOBALS['date_display_format'] == 0
// keep the year (so will simply be the original $date)
$newDate = $date;
}
return $newDate;
}
// this is case if the $date does not have 10 characters
return $date;
}
// 0 - Time format 24 hr
// 1 - Time format 12 hr
function oeFormatTime( $time, $format = "" )
{
$formatted = $time;
if ( $format == "" ) {
$format = $GLOBALS['time_display_format'];
}
if ( $format == 0 ) {
$formatted = date( "H:i", strtotime( $time ) );
} else if ( $format == 1 ) {
$formatted = date( "g:i a", strtotime( $time ) );
}
return $formatted;
}
// Format short date from time.
function oeFormatSDFT($time) {
return oeFormatShortDate(date('Y-m-d', $time));
}
// Format the body of a patient note.
function oeFormatPatientNote($note) {
$i = 0;
while ($i !== false) {
if (preg_match('/^\d\d\d\d-\d\d-\d\d/', substr($note, $i))) {
$note = substr($note, 0, $i) . oeFormatShortDate(substr($note, $i, 10)) . substr($note, $i + 10);
}
$i = strpos("\n", $note, $i);
if ($i !== false) ++$i;
}
return $note;
}
function oeFormatClientID($id) {
// TBD
return $id;
}
//----------------------------------------------------
function DateFormatRead()
{//For the 3 supported date format,the javascript code also should be twicked to display the date as per it.
//Output of this function is given to 'ifFormat' parameter of the 'Calendar.setup'.
//This will show the date as per the global settings.
if($GLOBALS['date_display_format']==0)
{
return "%Y-%m-%d";
}
else if($GLOBALS['date_display_format']==1)
{
return "%m/%d/%Y";
}
else if($GLOBALS['date_display_format']==2)
{
return "%d/%m/%Y";
}
}
function DateToYYYYMMDD($DateValue)
{//With the help of function DateFormatRead() now the user can enter date is any of the 3 formats depending upon the global setting.
//But in database the date can be stored only in the yyyy-mm-dd format.
//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.
if(trim($DateValue)=='')
{
return '';
}
if($GLOBALS['date_display_format']==0)
{
return $DateValue;
}
else if($GLOBALS['date_display_format']==1 || $GLOBALS['date_display_format']==2)
{
$DateValueArray=explode('/',$DateValue);
if($GLOBALS['date_display_format']==1)
{
return $DateValueArray[2].'-'.$DateValueArray[0].'-'.$DateValueArray[1];
}
if($GLOBALS['date_display_format']==2)
{
return $DateValueArray[2].'-'.$DateValueArray[1].'-'.$DateValueArray[0];
}
}
}
// Returns age in a desired format:
// 0 = "xx month(s)" if < 2 years, else years
// 1 = Years : just a number
// 2 = Months : just a number
// 3 = Gestational: "xx week(s) y day(s)"
// $dobYMD is YYYYMMDD or YYYY-MM-DD
// $nowYMD is same format but optional
//
function oeFormatAge($dobYMD, $nowYMD='', $format=0) {
// Strip any dashes from the dates.
$dobYMD = preg_replace('/-/', '', $dobYMD);
$nowYMD = preg_replace('/-/', '', $nowYMD);
$dobDay = substr($dobYMD,6,2);
$dobMonth = substr($dobYMD,4,2);
$dobYear = substr($dobYMD,0,4);
if ($nowYMD) {
$nowDay = substr($nowYMD,6,2);
$nowMonth = substr($nowYMD,4,2);
$nowYear = substr($nowYMD,0,4);
}
else {
$nowDay = date("d");
$nowMonth = date("m");
$nowYear = date("Y");
}
if ($format == 3) {
// Gestational age as weeks and days.
$secs = mktime(0, 0, 0, $nowMonth, $nowDay, $nowYear) -
mktime(0, 0, 0, $dobMonth, $dobDay, $dobYear);
$days = intval($secs / (24 * 60 * 60));
$weeks = intval($days / 7);
$days = $days % 7;
$age = "$weeks " . ($weeks == 1 ? xl('week') : xl('weeks')) .
" $days " . ($days == 1 ? xl('day' ) : xl('days' ));
}
else {
// Years or months.
$dayDiff = $nowDay - $dobDay;
$monthDiff = $nowMonth - $dobMonth;
$yearDiff = $nowYear - $dobYear;
$ageInMonths = $yearDiff * 12 + $monthDiff;
if ($dayDiff < 0) --$ageInMonths;
if ($format == 1 || ($format == 0 && $ageInMonths >= 24)) {
$age = $yearDiff;
if ($monthDiff < 0 || ($monthDiff == 0 && $dayDiff < 0)) --$age;
}
else {
$age = $ageInMonths;
if ($format == 0) {
$age .= ' ' . $ageInMonths == 1 ? xl('month') : xl('months');
}
}
}
return $age;
}
?>