diff --git a/core/lib/Drupal/Core/Datetime/DateFormatter.php b/core/lib/Drupal/Core/Datetime/DateFormatter.php index 02e8b92..a8b1338 100644 --- a/core/lib/Drupal/Core/Datetime/DateFormatter.php +++ b/core/lib/Drupal/Core/Datetime/DateFormatter.php @@ -196,7 +196,6 @@ public function format($timestamp, $type = 'medium', $format = '', $timezone = N * @see \Drupal\Core\Datetime\DateFormatter::formatDiff() */ public function formatInterval($interval, $granularity = 2, $langcode = NULL) { - $parts = 0; $output = ''; foreach ($this->units as $key => $value) { $key = explode('|', $key); @@ -204,9 +203,8 @@ public function formatInterval($interval, $granularity = 2, $langcode = NULL) { $output .= ($output ? ' ' : '') . $this->formatPlural(floor($interval / $value), $key[0], $key[1], array(), array('langcode' => $langcode)); $interval %= $value; $granularity--; - $parts++; } - elseif ($parts) { + elseif ($output) { // Break if there was previous output but not any output at this level, // to avoid skipping levels and getting output like "1 year 1 second". break; @@ -390,6 +388,11 @@ public function formatDiff($from, $to, $options = array()) { if ((!$output || $weeks > 0) && $granularity > 0 && $days > 0) { $interval_output .= ($interval_output ? ' ' : '') . $this->formatPlural($days, '1 day', '@count days', array(), array('langcode' => $options['langcode'])); } + else { + // If we have months but no weeks, explicitly set granularity to + // zero so we don't get output like "1 day 1 hour". + $granularity = 0; + } break; case 'h':