- // Handle whole hours.
- if (preg_match('/^([0-1]{0,1}[0-9]|2[0-4])h?$/', $value )) { // 0, 1 ... 24
- $normalized = trim($value, 'h');
- $normalized .= ':00';
- return $normalized;
- }
- // Handle already normalized value.
- if (preg_match('/^([0-1]{0,1}[0-9]|2[0-3]):?[0-5][0-9]$/', $value )) { // 0:00 - 23:59, 000 - 2359
- return $value;
- }
- // Handle a special case of 24:00.
- if ($value == '24:00') {
- return $value;
- }
- // Handle localized fractional hours.
- global $user;
- $localizedPattern = '/^([0-1]{0,1}[0-9]|2[0-3])?['.$user->decimal_mark.'][0-9]{1,4}h?$/';
- if (preg_match($localizedPattern, $value )) { // decimal values like 0.5, 1.25h, ... .. 23.9999h (or with comma)
- if ($user->decimal_mark == ',')
- $value = str_replace (',', '.', $value);
-
- $val = floatval($value);
- $mins = round($val * 60);
- $hours = (string)((int)($mins / 60));
- $mins = (string)($mins % 60);
- if (strlen($mins) == 1)
- $mins = '0' . $mins;
- return $hours.':'.$mins;
- }
- // Handle minutes.
- if (preg_match('/^\d{1,4}m$/', $value )) { // ddddm
- $mins = (int) trim($value, 'm');
- if ($mins > 1440) // More minutes than an entire day could hold.
- return false;
- $hours = (string)((int)($mins / 60));
- $mins = (string)($mins % 60);
- if (strlen($mins) == 1)
- $mins = '0' . $mins;
- return $hours.':'.$mins;
- }
- return false;
- }
-
- // normalizeDuration - converts a valid time duration string to format 00:00.
- static function normalizeDuration($value, $leadingZero = true) {
- $time_value = $value;
-
- // If we have a decimal format - convert to time format 00:00.
- global $user;
- if ($user->decimal_mark == ',')
- $time_value = str_replace (',', '.', $time_value);
-
- if((strpos($time_value, '.') !== false) || (strpos($time_value, 'h') !== false)) {
- $val = floatval($time_value);
- $mins = round($val * 60);
- $hours = (string)((int)($mins / 60));
- $mins = (string)($mins % 60);
- if ($leadingZero && strlen($hours) == 1)
- $hours = '0'.$hours;
- if (strlen($mins) == 1)
- $mins = '0' . $mins;
- return $hours.':'.$mins;
- }
-
- $time_a = explode(':', $time_value);
- $res = '';
-
- // 0-99
- if ((strlen($time_value) >= 1) && (strlen($time_value) <= 2) && !isset($time_a[1])) {
- $hours = $time_a[0];
- if ($leadingZero && strlen($hours) == 1)
- $hours = '0'.$hours;
- return $hours.':00';
- }
-
- // 000-2359 (2400)
- if ((strlen($time_value) >= 3) && (strlen($time_value) <= 4) && !isset($time_a[1])) {
- if (strlen($time_value)==3) $time_value = '0'.$time_value;
- $hours = substr($time_value,0,2);
- if ($leadingZero && strlen($hours) == 1)
- $hours = '0'.$hours;
- return $hours.':'.substr($time_value,2,2);
- }
-
- // 0:00-23:59 (24:00)
- if ((strlen($time_value) >= 4) && (strlen($time_value) <= 5) && isset($time_a[1])) {
- $hours = $time_a[0];
- if ($leadingZero && strlen($hours) == 1)
- $hours = '0'.$hours;
- return $hours.':'.$time_a[1];
- }