function _hms_seconds_to_formatted in HMS Field 7
Returns a formatted string form the number of seconds.
4 calls to _hms_seconds_to_formatted()
- hms_field_views_handler_filter_hms::value_form in views/
hms_field_views_handler_filter_hms.inc - Provide a simple textfield for equality
- theme_hms in ./
hms_field.module - Theme HMS
- theme_hms_natural_language in ./
hms_field.module - Theme hms_natural_language
- _hms_pre_render_form_element in ./
hms_field.module - Helper function to convert seconds to a formatted value (FORM API).
File
- ./
hms_field.module, line 568 - Provides an hms_field functionality.
Code
function _hms_seconds_to_formatted($seconds, $format = 'h:mm', $leading_zero = TRUE) {
// Return NULL on empty string.
if ($seconds === '') {
return NULL;
}
$factor = _hms_factor_map();
// We need factors, biggest first.
arsort($factor, SORT_NUMERIC);
$values = array();
$left_over = $seconds;
$str = '';
if ($seconds < 0) {
$str .= '-';
$left_over = abs($left_over);
}
// Space separated format
if ($format == 'hms') {
foreach ($factor as $key => $val) {
if ($left_over == 0) {
break;
}
$values[$key] = floor($left_over / $factor[$key]);
if ($values[$key]) {
$left_over -= $values[$key] * $factor[$key];
$str .= $values[$key] . $key . ' ';
}
}
}
else {
foreach ($factor as $key => $val) {
if (strpos($format, $key) === FALSE) {
continue;
// Not in our format, please go on, so we can plus this on a value in our format.
}
if ($left_over == 0) {
$values[$key] = 0;
continue;
}
$values[$key] = floor($left_over / $factor[$key]);
$left_over -= $values[$key] * $factor[$key];
}
$format = explode(':', $format);
foreach ($format as $key) {
if (!$leading_zero && (empty($values[substr($key, 0, 1)]) || !$values[substr($key, 0, 1)])) {
continue;
}
$leading_zero = TRUE;
$str .= sprintf('%0' . strlen($key) . 'd', $values[substr($key, 0, 1)]) . ':';
}
if (!strlen($str)) {
$key = array_pop($format);
$str = sprintf('%0' . strlen($key) . 'd', 0) . ':';
}
}
return substr($str, 0, -1);
}