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)
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";
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) {
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 = substr($date, 0, 10);
60 // this is case if the $date does not have 10 characters
64 // 0 - Time format 24 hr
65 // 1 - Time format 12 hr
66 function oeFormatTime($time, $format = "global", $seconds = false)
74 if ($format === "global") {
75 $format = $GLOBALS['time_display_format'];
81 $formatted = date("g:i:s a", strtotime($time));
83 $formatted = date("g:i a", strtotime($time));
85 } else { // ($format == 0)
87 $formatted = date("H:i:s", strtotime($time));
89 $formatted = date("H:i", strtotime($time));
97 * Returns the complete formatted datetime string according the global date and time format
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
111 function oeTimestampFormatDateTime($timestamp)
114 $timestamp = strtotime(date('Y-m-d H:i'));
117 if ($GLOBALS['time_display_format'] == 0) {
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);
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)
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($note, "\n", $i);
158 function oeFormatClientID($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') {
174 } elseif ($mode == 'validateJS') {
176 } else { //$mode=='jquery-datetimepicker'
179 } else if ($GLOBALS['date_display_format']==1) {
180 if ($mode == 'legacy') {
182 } elseif ($mode == 'validateJS') {
184 } else { //$mode=='jquery-datetimepicker'
187 } else if ($GLOBALS['date_display_format']==2) {
188 if ($mode == 'legacy') {
190 } elseif ($mode == 'validateJS') {
192 } else { //$mode=='jquery-datetimepicker'
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)=='') {
207 if ($GLOBALS['date_display_format']==0) {
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)=='') {
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)) {
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);
269 $nowDay = substr($nowYMD, 6, 2);
270 $nowMonth = substr($nowYMD, 4, 2);
271 $nowYear = substr($nowYMD, 0, 4);
274 $nowMonth = date("m");
275 $nowYear = date("Y");
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);
285 $age = "$weeks " . ($weeks == 1 ?
xl('week') : xl('weeks')) .
286 " $days " . ($days == 1 ?
xl('day') : xl('days'));
289 $dayDiff = $nowDay - $dobDay;
290 $monthDiff = $nowMonth - $dobMonth;
291 $yearDiff = $nowYear - $dobYear;
292 $ageInMonths = $yearDiff * 12 +
$monthDiff;
297 if ($format == 1 ||
($format == 0 && $ageInMonths >= 24)) {
299 if ($monthDiff < 0 ||
($monthDiff == 0 && $dayDiff < 0)) {
305 $age .= ' ' . $ageInMonths == 1 ?
xl('month') : xl('months');