Merge pull request #1403 from bradymiller/matrix-israel-formatDate
[openemr.git] / library / formatting.inc.php
blob4f448b408f1d9cad9801276e4ba82a65393afa96
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")
63 if (empty($time)) {
64 return "";
66 $formatted = $time;
67 if ($format === "global") {
68 $format = $GLOBALS['time_display_format'];
71 if ($format == 0) {
72 $formatted = date("H:i", strtotime($time));
73 } else if ($format == 1) {
74 $formatted = date("g:i a", strtotime($time));
77 return $formatted;
80 /**
81 * Returns the complete formatted datetime string according the global date and time format
82 * @param $datetime
83 * @return string
85 function oeFormatDateTime($datetime, $formatTime = "global")
87 return oeFormatShortDate(substr($datetime, 0, 10)) . " " . oeFormatTime(substr($datetime, 11), $formatTime);
90 /**
91 * Returns the complete formatted datetime string according the global date and time format
92 * @param $timestamp
93 * @return string
95 function oeTimestampFormatDateTime($timestamp)
97 if (!$timestamp) {
98 $timestamp = strtotime(date('Y-m-d H:i'));
101 if ($GLOBALS['time_display_format'] == 0) {
102 $timeFormat = 'H:i';
103 } else { // $GLOBALS['time_display_format'] == 1
104 $timeFormat = 'g:i a';
107 if ($GLOBALS['date_display_format'] == 1) { // mm/dd/yyyy
108 $newDate = date('m/d/Y ' . $timeFormat, $timestamp);
109 } else if ($GLOBALS['date_display_format'] == 2) { // dd/mm/yyyy
110 $newDate = date('d/m/Y ' . $timeFormat, $timestamp);
111 } else { // yyyy-mm-dd
112 $newDate = date('Y-m-d ' . $timeFormat, $timestamp);
115 return $newDate;
118 // Format short date from time.
119 function oeFormatSDFT($time)
121 return oeFormatShortDate(date('Y-m-d', $time));
124 // Format the body of a patient note.
125 function oeFormatPatientNote($note)
127 $i = 0;
128 while ($i !== false) {
129 if (preg_match('/^\d\d\d\d-\d\d-\d\d/', substr($note, $i))) {
130 $note = substr($note, 0, $i) . oeFormatShortDate(substr($note, $i, 10)) . substr($note, $i + 10);
133 $i = strpos("\n", $note, $i);
134 if ($i !== false) {
135 ++$i;
139 return $note;
142 function oeFormatClientID($id)
145 // TBD
147 return $id;
149 //----------------------------------------------------
150 function DateFormatRead($mode = 'legacy')
152 //For the 3 supported date format,the javascript code also should be twicked to display the date as per it.
153 //Output of this function is given to 'ifFormat' parameter of the 'Calendar.setup'.
154 //This will show the date as per the global settings.
155 if ($GLOBALS['date_display_format']==0) {
156 if ($mode == 'legacy') {
157 return "%Y-%m-%d";
158 } elseif ($mode == 'validateJS') {
159 return "YYYY-MM-DD";
160 } else { //$mode=='jquery-datetimepicker'
161 return "Y-m-d";
163 } else if ($GLOBALS['date_display_format']==1) {
164 if ($mode == 'legacy') {
165 return "%m/%d/%Y";
166 } elseif ($mode == 'validateJS') {
167 return "MM/DD/YYYY";
168 } else { //$mode=='jquery-datetimepicker'
169 return "m/d/Y";
171 } else if ($GLOBALS['date_display_format']==2) {
172 if ($mode == 'legacy') {
173 return "%d/%m/%Y";
174 } elseif ($mode == 'validateJS') {
175 return "DD/MM/YYYY";
176 } else { //$mode=='jquery-datetimepicker'
177 return "d/m/Y";
182 function DateToYYYYMMDD($DateValue)
184 //With the help of function DateFormatRead() now the user can enter date is any of the 3 formats depending upon the global setting.
185 //But in database the date can be stored only in the yyyy-mm-dd format.
186 //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.
187 if (trim($DateValue)=='') {
188 return '';
191 if ($GLOBALS['date_display_format']==0) {
192 return $DateValue;
193 } else if ($GLOBALS['date_display_format']==1 || $GLOBALS['date_display_format']==2) {
194 $DateValueArray=explode('/', $DateValue);
195 if ($GLOBALS['date_display_format']==1) {
196 return $DateValueArray[2].'-'.$DateValueArray[0].'-'.$DateValueArray[1];
199 if ($GLOBALS['date_display_format']==2) {
200 return $DateValueArray[2].'-'.$DateValueArray[1].'-'.$DateValueArray[0];
205 function TimeToHHMMSS($TimeValue)
207 //For now, just return the $TimeValue, since input fields are not formatting time.
208 // This can be upgraded if decided to format input time fields.
210 if (trim($TimeValue)=='') {
211 return '';
214 return $TimeValue;
218 function DateTimeToYYYYMMDDHHMMSS($DateTimeValue)
220 //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.
222 // First deal with the date
223 $fixed_date = DateToYYYYMMDD(substr($DateTimeValue, 0, 10));
225 // Then deal with the time
226 $fixed_time = TimeToHHMMSS(substr($DateTimeValue, 11));
228 return $fixed_date . " " . $fixed_time;
231 // Returns age in a desired format:
232 // 0 = "xx month(s)" if < 2 years, else years
233 // 1 = Years : just a number
234 // 2 = Months : just a number
235 // 3 = Gestational: "xx week(s) y day(s)"
236 // $dobYMD is YYYYMMDD or YYYY-MM-DD
237 // $nowYMD is same format but optional
239 function oeFormatAge($dobYMD, $nowYMD = '', $format = 0)
241 // Strip any dashes from the dates.
242 $dobYMD = preg_replace('/-/', '', $dobYMD);
243 $nowYMD = preg_replace('/-/', '', $nowYMD);
244 $dobDay = substr($dobYMD, 6, 2);
245 $dobMonth = substr($dobYMD, 4, 2);
246 $dobYear = substr($dobYMD, 0, 4);
248 if ($nowYMD) {
249 $nowDay = substr($nowYMD, 6, 2);
250 $nowMonth = substr($nowYMD, 4, 2);
251 $nowYear = substr($nowYMD, 0, 4);
252 } else {
253 $nowDay = date("d");
254 $nowMonth = date("m");
255 $nowYear = date("Y");
258 if ($format == 3) {
259 // Gestational age as weeks and days.
260 $secs = mktime(0, 0, 0, $nowMonth, $nowDay, $nowYear) -
261 mktime(0, 0, 0, $dobMonth, $dobDay, $dobYear);
262 $days = intval($secs / (24 * 60 * 60));
263 $weeks = intval($days / 7);
264 $days = $days % 7;
265 $age = "$weeks " . ($weeks == 1 ? xl('week') : xl('weeks')) .
266 " $days " . ($days == 1 ? xl('day') : xl('days'));
267 } else {
268 // Years or months.
269 $dayDiff = $nowDay - $dobDay;
270 $monthDiff = $nowMonth - $dobMonth;
271 $yearDiff = $nowYear - $dobYear;
272 $ageInMonths = $yearDiff * 12 + $monthDiff;
273 if ($dayDiff < 0) {
274 --$ageInMonths;
277 if ($format == 1 || ($format == 0 && $ageInMonths >= 24)) {
278 $age = $yearDiff;
279 if ($monthDiff < 0 || ($monthDiff == 0 && $dayDiff < 0)) {
280 --$age;
282 } else {
283 $age = $ageInMonths;
284 if ($format == 0) {
285 $age .= ' ' . $ageInMonths == 1 ? xl('month') : xl('months');
290 return $age;