Hello,

one os the projects I am working includes only video items with short durations, mostly in 1-10 minutes.

How could we set the display to only show minutes and seconds MM:SS, excluding HH hours.

Thank you.

Comments

naero’s picture

Here ya go:

// Reformat the colon formatter to hide hours if they aren't used (that is, 0)
function themename_duration_formatter_colons($element) {
  if (isset($element['#node']->duration)) { // loaded by duration_handler_field
    $element['#item']['duration'] = $element['#node']->duration;
  }
  // return duration_format_custom($element['#item']['duration'], '%h:%M:%S');
  $hours = intval($element['#item']['duration']->get_hours());
  if ( $hours <= 0 ) {
    return duration_format_custom($element['#item']['duration'], '%M:%S');
  } else {
    return duration_format_custom($element['#item']['duration'], '%h:%M:%S');
  }
}

Replace themename with the name of your theme. Place this code in your template.php file.

naero’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

doublejosh’s picture

This works great, but fails when a piece of content DOES NOT have a duration, because it's not an object.
You need to put the get_hours() function inside the test for a duration value.

function MYTHEME_duration_formatter_colons($element) {
  if (isset($element['#node']->duration)) { // loaded by duration_handler_field
    $element['#item']['duration'] = $element['#node']->duration;
    $hours = intval($element['#item']['duration']->get_hours());
  }
  // return duration_format_custom($element['#item']['duration'], '%h:%M:%S');
  if ( $hours <= 0 ) {
    return duration_format_custom($element['#item']['duration'], '%M:%S');
  } else {
    return duration_format_custom($element['#item']['duration'], '%h:%M:%S');
  }
}
doublejosh’s picture

Pushed it just a bit farther. This will drop off seconds if they aren't tracked in the node type, show days when recorded, or just hours if no days are present in the value. Going much farther and it would really need a more comprehensive switch statement for the display.

function MYTHEME_duration_formatter_colons($element) {
  if (isset($element['#node']->duration)) { // loaded by duration_handler_field
    $element['#item']['duration'] = $element['#node']->duration;
  }
  if($element['#item']['duration']) {
    $mins = intval( $element['#item']['duration']->get_minutes() );
    $hours = intval( $element['#item']['duration']->get_hours() );
    $days = intval( $element['#item']['duration']->get_days() );
  }
  // Overwritten original output...
  // return duration_format_custom($element['#item']['duration'], '%h:%M:%S');
  $formater = '';
  if ($days > 0) $formater = '%e days '; // Include days.
  if($mins > 0 || $hours > 0) { // If there are any lower units add those in.
    $formater .= ($hours <= 0) ? '%M' : '%h:%M';
    if($element['#item']['duration']->duration['seconds']) $formater .= ':%S'; // Are seconds tracked?
  }
  return duration_format_custom($element['#item']['duration'], $formater);
}