clinical report datetime support
[openemr.git] / library / formatting.inc.php
blob90efd0d80647508486864d87088af7351a9bb1bb
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(
12 floatval($amount),
13 $GLOBALS['currency_decimals'],
14 $GLOBALS['currency_dec_point'],
15 $GLOBALS['currency_thousands_sep']
17 // If the currency symbol exists and is requested, prepend it.
18 if ($symbol && !empty($GLOBALS['gbl_currency_symbol'])) {
19 $s = $GLOBALS['gbl_currency_symbol'] . " $s";
22 return $s;
25 function oeFormatShortDate($date = 'today', $showYear = true)
27 if ($date === 'today') {
28 $date = date('Y-m-d');
31 if (strlen($date) >= 10) {
32 // assume input is yyyy-mm-dd
33 if ($GLOBALS['date_display_format'] == 1) { // mm/dd/yyyy, note year is added below
34 $newDate = substr($date, 5, 2) . '/' . substr($date, 8, 2);
35 } else if ($GLOBALS['date_display_format'] == 2) { // dd/mm/yyyy, note year is added below
36 $newDate = substr($date, 8, 2) . '/' . substr($date, 5, 2);
39 // process the year (add for formats 1 and 2; remove for format 0)
40 if ($GLOBALS['date_display_format'] == 1 || $GLOBALS['date_display_format'] == 2) {
41 if ($showYear) {
42 $newDate .= '/' . substr($date, 0, 4);
44 } else if (!$showYear) { // $GLOBALS['date_display_format'] == 0
45 // need to remove the year
46 $newDate = substr($date, 5, 2) . '-' . substr($date, 8, 2);
47 } else { // $GLOBALS['date_display_format'] == 0
48 // keep the year (so will simply be the original $date)
49 $newDate = $date;
52 return $newDate;
55 // this is case if the $date does not have 10 characters
56 return $date;
59 // 0 - Time format 24 hr
60 // 1 - Time format 12 hr
61 function oeFormatTime($time, $format = "global", $seconds = false)
63 if (empty($time)) {
64 return "";
67 $formatted = $time;
69 if ($format === "global") {
70 $format = $GLOBALS['time_display_format'];
74 if ($format == 1) {
75 if ($seconds) {
76 $formatted = date("g:i:s a", strtotime($time));
77 } else {
78 $formatted = date("g:i a", strtotime($time));
80 } else { // ($format == 0)
81 if ($seconds) {
82 $formatted = date("H:i:s", strtotime($time));
83 } else {
84 $formatted = date("H:i", strtotime($time));
88 return $formatted;
91 /**
92 * Returns the complete formatted datetime string according the global date and time format
93 * @param $datetime
94 * @return string
96 function oeFormatDateTime($datetime, $formatTime = "global", $seconds = false)
98 return oeFormatShortDate(substr($datetime, 0, 10)) . " " . oeFormatTime(substr($datetime, 11), $formatTime, $seconds);
102 * Returns the complete formatted datetime string according the global date and time format
103 * @param $timestamp
104 * @return string
106 function oeTimestampFormatDateTime($timestamp)
108 if (!$timestamp) {
109 $timestamp = strtotime(date('Y-m-d H:i'));
112 if ($GLOBALS['time_display_format'] == 0) {
113 $timeFormat = 'H:i';
114 } else { // $GLOBALS['time_display_format'] == 1
115 $timeFormat = 'g:i a';
118 if ($GLOBALS['date_display_format'] == 1) { // mm/dd/yyyy
119 $newDate = date('m/d/Y ' . $timeFormat, $timestamp);
120 } else if ($GLOBALS['date_display_format'] == 2) { // dd/mm/yyyy
121 $newDate = date('d/m/Y ' . $timeFormat, $timestamp);
122 } else { // yyyy-mm-dd
123 $newDate = date('Y-m-d ' . $timeFormat, $timestamp);
126 return $newDate;
129 // Format short date from time.
130 function oeFormatSDFT($time)
132 return oeFormatShortDate(date('Y-m-d', $time));
135 // Format the body of a patient note.
136 function oeFormatPatientNote($note)
138 $i = 0;
139 while ($i !== false) {
140 if (preg_match('/^\d\d\d\d-\d\d-\d\d/', substr($note, $i))) {
141 $note = substr($note, 0, $i) . oeFormatShortDate(substr($note, $i, 10)) . substr($note, $i + 10);
144 $i = strpos("\n", $note, $i);
145 if ($i !== false) {
146 ++$i;
150 return $note;
153 function oeFormatClientID($id)
156 // TBD
158 return $id;
160 //----------------------------------------------------
161 function DateFormatRead($mode = 'legacy')
163 //For the 3 supported date format,the javascript code also should be twicked to display the date as per it.
164 //Output of this function is given to 'ifFormat' parameter of the 'Calendar.setup'.
165 //This will show the date as per the global settings.
166 if ($GLOBALS['date_display_format']==0) {
167 if ($mode == 'legacy') {
168 return "%Y-%m-%d";
169 } elseif ($mode == 'validateJS') {
170 return "YYYY-MM-DD";
171 } else { //$mode=='jquery-datetimepicker'
172 return "Y-m-d";
174 } else if ($GLOBALS['date_display_format']==1) {
175 if ($mode == 'legacy') {
176 return "%m/%d/%Y";
177 } elseif ($mode == 'validateJS') {
178 return "MM/DD/YYYY";
179 } else { //$mode=='jquery-datetimepicker'
180 return "m/d/Y";
182 } else if ($GLOBALS['date_display_format']==2) {
183 if ($mode == 'legacy') {
184 return "%d/%m/%Y";
185 } elseif ($mode == 'validateJS') {
186 return "DD/MM/YYYY";
187 } else { //$mode=='jquery-datetimepicker'
188 return "d/m/Y";
193 function DateToYYYYMMDD($DateValue)
195 //With the help of function DateFormatRead() now the user can enter date is any of the 3 formats depending upon the global setting.
196 //But in database the date can be stored only in the yyyy-mm-dd format.
197 //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.
198 if (trim($DateValue)=='') {
199 return '';
202 if ($GLOBALS['date_display_format']==0) {
203 return $DateValue;
204 } else if ($GLOBALS['date_display_format']==1 || $GLOBALS['date_display_format']==2) {
205 $DateValueArray=explode('/', $DateValue);
206 if ($GLOBALS['date_display_format']==1) {
207 return $DateValueArray[2].'-'.$DateValueArray[0].'-'.$DateValueArray[1];
210 if ($GLOBALS['date_display_format']==2) {
211 return $DateValueArray[2].'-'.$DateValueArray[1].'-'.$DateValueArray[0];
216 function TimeToHHMMSS($TimeValue)
218 //For now, just return the $TimeValue, since input fields are not formatting time.
219 // This can be upgraded if decided to format input time fields.
221 if (trim($TimeValue)=='') {
222 return '';
225 return $TimeValue;
229 function DateTimeToYYYYMMDDHHMMSS($DateTimeValue)
231 //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.
233 // First deal with the date
234 $fixed_date = DateToYYYYMMDD(substr($DateTimeValue, 0, 10));
236 // Then deal with the time
237 $fixed_time = TimeToHHMMSS(substr($DateTimeValue, 11));
239 if (empty($fixed_date) && empty($fixed_time)) {
240 return "";
241 } else {
242 return $fixed_date . " " . $fixed_time;
246 // Returns age in a desired format:
247 // 0 = "xx month(s)" if < 2 years, else years
248 // 1 = Years : just a number
249 // 2 = Months : just a number
250 // 3 = Gestational: "xx week(s) y day(s)"
251 // $dobYMD is YYYYMMDD or YYYY-MM-DD
252 // $nowYMD is same format but optional
254 function oeFormatAge($dobYMD, $nowYMD = '', $format = 0)
256 // Strip any dashes from the dates.
257 $dobYMD = preg_replace('/-/', '', $dobYMD);
258 $nowYMD = preg_replace('/-/', '', $nowYMD);
259 $dobDay = substr($dobYMD, 6, 2);
260 $dobMonth = substr($dobYMD, 4, 2);
261 $dobYear = substr($dobYMD, 0, 4);
263 if ($nowYMD) {
264 $nowDay = substr($nowYMD, 6, 2);
265 $nowMonth = substr($nowYMD, 4, 2);
266 $nowYear = substr($nowYMD, 0, 4);
267 } else {
268 $nowDay = date("d");
269 $nowMonth = date("m");
270 $nowYear = date("Y");
273 if ($format == 3) {
274 // Gestational age as weeks and days.
275 $secs = mktime(0, 0, 0, $nowMonth, $nowDay, $nowYear) -
276 mktime(0, 0, 0, $dobMonth, $dobDay, $dobYear);
277 $days = intval($secs / (24 * 60 * 60));
278 $weeks = intval($days / 7);
279 $days = $days % 7;
280 $age = "$weeks " . ($weeks == 1 ? xl('week') : xl('weeks')) .
281 " $days " . ($days == 1 ? xl('day') : xl('days'));
282 } else {
283 // Years or months.
284 $dayDiff = $nowDay - $dobDay;
285 $monthDiff = $nowMonth - $dobMonth;
286 $yearDiff = $nowYear - $dobYear;
287 $ageInMonths = $yearDiff * 12 + $monthDiff;
288 if ($dayDiff < 0) {
289 --$ageInMonths;
292 if ($format == 1 || ($format == 0 && $ageInMonths >= 24)) {
293 $age = $yearDiff;
294 if ($monthDiff < 0 || ($monthDiff == 0 && $dayDiff < 0)) {
295 --$age;
297 } else {
298 $age = $ageInMonths;
299 if ($format == 0) {
300 $age .= ' ' . $ageInMonths == 1 ? xl('month') : xl('months');
305 return $age;