7 * @link https://www.open-emr.org
8 * @author Rod Roark <rod@sunsetsystems.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2010-2014 Rod Roark <rod@sunsetsystems.com>
11 * @copyright Copyright (c) 2017-2018 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 use OpenEMR\Common\Utils\FormatMoney
;
16 use OpenEMR\Services\Utils\DateFormatterUtils
;
18 // TODO: look at moving all of the date functions into the DateFormatterUtils class.
20 function oeFormatMoney($amount, $symbol = false)
22 return FormatMoney
::getFormattedMoney($amount, $symbol);
25 function oeFormatShortDate($date = 'today', $showYear = true)
27 return DateFormatterUtils
::oeFormatShortDate($date, $showYear);
30 // 0 - Time format 24 hr
31 // 1 - Time format 12 hr
32 function oeFormatTime($time, $format = "global", $seconds = false)
40 if ($format === "global") {
41 $format = $GLOBALS['time_display_format'];
47 $formatted = date("g:i:s a", strtotime($time));
49 $formatted = date("g:i a", strtotime($time));
51 } else { // ($format == 0)
53 $formatted = date("H:i:s", strtotime($time));
55 $formatted = date("H:i", strtotime($time));
63 * Returns the complete formatted datetime string according the global date and time format
67 function oeFormatDateTime($datetime, $formatTime = "global", $seconds = false)
69 return oeFormatShortDate(substr($datetime ??
'', 0, 10)) . " " . oeFormatTime(substr($datetime ??
'', 11), $formatTime, $seconds);
73 * Returns the complete formatted datetime string according the global date and time format
77 function oeTimestampFormatDateTime($timestamp)
80 $timestamp = strtotime(date('Y-m-d H:i'));
83 if ($GLOBALS['time_display_format'] == 0) {
85 } else { // $GLOBALS['time_display_format'] == 1
86 $timeFormat = 'g:i a';
89 if ($GLOBALS['date_display_format'] == 1) { // mm/dd/yyyy
90 $newDate = date('m/d/Y ' . $timeFormat, $timestamp);
91 } elseif ($GLOBALS['date_display_format'] == 2) { // dd/mm/yyyy
92 $newDate = date('d/m/Y ' . $timeFormat, $timestamp);
93 } else { // yyyy-mm-dd
94 $newDate = date('Y-m-d ' . $timeFormat, $timestamp);
100 // Format short date from time.
101 function oeFormatSDFT($time)
103 return oeFormatShortDate(date('Y-m-d', $time));
106 // Format the body of a patient note.
107 function oeFormatPatientNote($note)
110 while ($i !== false) {
111 if (preg_match('/^\d\d\d\d-\d\d-\d\d/', substr($note, $i))) {
112 $note = substr($note, 0, $i) . oeFormatShortDate(substr($note, $i, 10)) . substr($note, $i +
10);
115 $i = strpos($note, "\n", $i);
124 function oeFormatClientID($id)
131 //----------------------------------------------------
132 function DateFormatRead($mode = 'legacy')
134 //For the 3 supported date format,the javascript code also should be twicked to display the date as per it.
135 //Output of this function is given to 'ifFormat' parameter of the 'Calendar.setup'.
136 //This will show the date as per the global settings.
137 if ($GLOBALS['date_display_format'] == 0) {
138 if ($mode == 'legacy') {
140 } elseif ($mode == 'validateJS') {
142 } else { //$mode=='jquery-datetimepicker'
145 } elseif ($GLOBALS['date_display_format'] == 1) {
146 if ($mode == 'legacy') {
148 } elseif ($mode == 'validateJS') {
150 } else { //$mode=='jquery-datetimepicker'
153 } elseif ($GLOBALS['date_display_format'] == 2) {
154 if ($mode == 'legacy') {
156 } elseif ($mode == 'validateJS') {
158 } else { //$mode=='jquery-datetimepicker'
164 function DateToYYYYMMDD($DateValue)
166 return DateFormatterUtils
::DateToYYYYMMDD($DateValue);
169 function TimeToHHMMSS($TimeValue)
171 if (trim($TimeValue) == '') {
175 $date = new DateTimeImmutable('1970-01-01' . $TimeValue);
176 return $date->format('H:i:s');
180 function DateTimeToYYYYMMDDHHMMSS($DateTimeValue)
182 //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.
184 // First deal with the date
185 $fixed_date = DateToYYYYMMDD(substr($DateTimeValue, 0, 10));
187 // Then deal with the time
188 $fixed_time = TimeToHHMMSS(substr($DateTimeValue, 11));
190 if (empty($fixed_date) && empty($fixed_time)) {
193 return $fixed_date . " " . $fixed_time;
197 // Returns age in a desired format:
198 // 0 = "xx month(s)" if < 2 years, else years
199 // 1 = Years : just a number
200 // 2 = Months : just a number
201 // 3 = Gestational: "xx week(s) y day(s)"
202 // $dobYMD is YYYYMMDD or YYYY-MM-DD
203 // $nowYMD is same format but optional
205 function oeFormatAge($dobYMD, $nowYMD = '', $format = 0)
207 // Strip any dashes from the dates.
208 $dobYMD = preg_replace('/-/', '', $dobYMD);
209 $nowYMD = preg_replace('/-/', '', $nowYMD);
210 $dobDay = substr($dobYMD, 6, 2);
211 $dobMonth = substr($dobYMD, 4, 2);
212 $dobYear = substr($dobYMD, 0, 4);
215 $nowDay = substr($nowYMD, 6, 2);
216 $nowMonth = substr($nowYMD, 4, 2);
217 $nowYear = substr($nowYMD, 0, 4);
220 $nowMonth = date("m");
221 $nowYear = date("Y");
225 // Gestational age as weeks and days.
226 $secs = mktime(0, 0, 0, $nowMonth, $nowDay, $nowYear) -
227 mktime(0, 0, 0, $dobMonth, $dobDay, $dobYear);
228 $days = intval($secs / (24 * 60 * 60));
229 $weeks = intval($days / 7);
231 $age = "$weeks " . ($weeks == 1 ?
xl('week') : xl('weeks')) .
232 " $days " . ($days == 1 ?
xl('day') : xl('days'));
235 $dayDiff = $nowDay - $dobDay;
236 $monthDiff = $nowMonth - $dobMonth;
237 $yearDiff = $nowYear - $dobYear;
238 $ageInMonths = $yearDiff * 12 +
$monthDiff;
243 if ($format == 1 ||
($format == 0 && $ageInMonths >= 24)) {
245 if ($monthDiff < 0 ||
($monthDiff == 0 && $dayDiff < 0)) {
251 $age .= ' ' . $ageInMonths == 1 ?
xl('month') : xl('months');