diff --git a/core/lib/Drupal/Core/Datetime/DateFormatter.php b/core/lib/Drupal/Core/Datetime/DateFormatter.php index 073c956..34fcfa0 100644 --- a/core/lib/Drupal/Core/Datetime/DateFormatter.php +++ b/core/lib/Drupal/Core/Datetime/DateFormatter.php @@ -357,6 +357,7 @@ public function formatDiff($from, $to, $options = array()) { $interval = $date_time_to->diff($date_time_from); $granularity = $options['granularity']; + $parts = 0; $output = ''; // We loop over the keys provided by \DateInterval explicitly. Since we @@ -380,12 +381,21 @@ public function formatDiff($from, $to, $options = array()) { // ourselves. $interval_output = ''; $days = $interval->d; + $weeks = floor($days / 7); if ($days >= 7) { - $weeks = floor($days / 7); $interval_output .= $this->formatPlural($weeks, '1 week', '@count weeks', array(), array('langcode' => $options['langcode'])); $days -= $weeks * 7; $granularity--; } + + // If months already were found and there are no weeks, explicitly + // break out of both the switch and the foreach loop to ensure only + // adjacent parts are added to the output. This avoids getting + // output like "1 month 1 day". + if($parts > 0 && $weeks == 0) { + break 2; + } + if ($granularity > 0 && $days > 0) { $interval_output .= ($interval_output ? ' ' : '') . $this->formatPlural($days, '1 day', '@count days', array(), array('langcode' => $options['langcode'])); } @@ -406,6 +416,12 @@ public function formatDiff($from, $to, $options = array()) { } $output .= ($output ? ' ' : '') . $interval_output; $granularity--; + $parts++; + } + elseif ($parts) { + // 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; } if ($granularity == 0) { diff --git a/core/tests/Drupal/Tests/Core/Datetime/DateTest.php b/core/tests/Drupal/Tests/Core/Datetime/DateTest.php index 85e3ae4..e767914 100644 --- a/core/tests/Drupal/Tests/Core/Datetime/DateTest.php +++ b/core/tests/Drupal/Tests/Core/Datetime/DateTest.php @@ -310,8 +310,8 @@ public function providerTestFormatDiff() { // Checks for hours and possibly minutes or seconds. array('1 hour', $this->createTimestamp('2013-12-11 09:09:08'), $request_time), array('1 hour', $this->createTimestamp('2013-12-11 09:09:08'), $request_time), - array('1 hour 1 second', $this->createTimestamp('2013-12-11 09:09:07'), $request_time), - array('1 hour 2 seconds', $this->createTimestamp('2013-12-11 09:09:06'), $request_time), + array('1 hour', $this->createTimestamp('2013-12-11 09:09:07'), $request_time), + array('1 hour', $this->createTimestamp('2013-12-11 09:09:06'), $request_time), array('1 hour 1 minute', $this->createTimestamp('2013-12-11 09:08:08'), $request_time), array('1 hour 1 minute 1 second', $this->createTimestamp('2013-12-11 09:08:07'), $request_time, $granularity_3), array('1 hour 1 minute 2 seconds', $this->createTimestamp('2013-12-11 09:08:06'), $request_time, $granularity_4), @@ -321,13 +321,13 @@ public function providerTestFormatDiff() { // Checks for days and possibly hours, minutes or seconds. array('1 day', $this->createTimestamp('2013-12-10 10:09:08'), $request_time), - array('1 day 1 second', $this->createTimestamp('2013-12-10 10:09:07'), $request_time), + array('1 day', $this->createTimestamp('2013-12-10 10:09:07'), $request_time), array('1 day 1 hour', $this->createTimestamp('2013-12-10 09:09:08'), $request_time), array('1 day 1 hour 1 minute', $this->createTimestamp('2013-12-10 09:08:07'), $request_time, $granularity_3 + $langcode_en), array('1 day 1 hour 1 minute 1 second', $this->createTimestamp('2013-12-10 09:08:07'), $request_time, $granularity_4 + $langcode_lolspeak), array('1 day 2 hours 2 minutes 2 seconds', $this->createTimestamp('2013-12-10 08:07:06'), $request_time, $granularity_4), array('2 days', $this->createTimestamp('2013-12-09 10:09:08'), $request_time), - array('2 days 2 minutes', $this->createTimestamp('2013-12-09 10:07:08'), $request_time), + array('2 days', $this->createTimestamp('2013-12-09 10:07:08'), $request_time), array('2 days 2 hours', $this->createTimestamp('2013-12-09 08:09:08'), $request_time), array('2 days 2 hours 2 minutes', $this->createTimestamp('2013-12-09 08:07:06'), $request_time, $granularity_3 + $langcode_en), array('2 days 2 hours 2 minutes 2 seconds', $this->createTimestamp('2013-12-09 08:07:06'), $request_time, $granularity_4 + $langcode_lolspeak), @@ -343,33 +343,33 @@ public function providerTestFormatDiff() { // Checks for months and possibly days, hours, minutes or seconds. array('1 month', $this->createTimestamp('2013-11-11 10:09:08'), $request_time), - array('1 month 1 second', $this->createTimestamp('2013-11-11 10:09:07'), $request_time), - array('1 month 1 hour', $this->createTimestamp('2013-11-11 09:09:08'), $request_time), - array('1 month 1 hour 1 minute', $this->createTimestamp('2013-11-11 09:08:07'), $request_time, $granularity_3), - array('1 month 1 hour 1 minute 1 second', $this->createTimestamp('2013-11-11 09:08:07'), $request_time, $granularity_4), + array('1 month', $this->createTimestamp('2013-11-11 10:09:07'), $request_time), + array('1 month', $this->createTimestamp('2013-11-11 09:09:08'), $request_time), + array('1 month', $this->createTimestamp('2013-11-11 09:08:07'), $request_time, $granularity_3), + array('1 month', $this->createTimestamp('2013-11-11 09:08:07'), $request_time, $granularity_4), array('1 month 4 weeks', $this->createTimestamp('2013-10-13 10:09:08'), $request_time), array('1 month 4 weeks 1 day', $this->createTimestamp('2013-10-13 10:09:08'), $request_time, $granularity_3), array('1 month 4 weeks', $this->createTimestamp('2013-10-12 10:09:08'), $request_time), array('1 month 4 weeks 2 days', $this->createTimestamp('2013-10-12 10:09:08'), $request_time, $granularity_3), array('2 months', $this->createTimestamp('2013-10-11 10:09:08'), $request_time), - array('2 months 1 day', $this->createTimestamp('2013-10-10 10:09:08'), $request_time), - array('2 months 2 days', $this->createTimestamp('2013-10-09 08:07:06'), $request_time), - array('2 months 2 days 2 hours', $this->createTimestamp('2013-10-09 08:07:06'), $request_time, $granularity_3), - array('2 months 2 days 2 hours 2 minutes', $this->createTimestamp('2013-10-09 08:07:06'), $request_time, $granularity_4), - array('6 months 2 days', $this->createTimestamp('2013-06-09 10:09:08'), $request_time), - array('11 months 3 hours', $this->createTimestamp('2013-01-11 07:09:08'), $request_time), + array('2 months', $this->createTimestamp('2013-10-10 10:09:08'), $request_time), + array('2 months', $this->createTimestamp('2013-10-09 08:07:06'), $request_time), + array('2 months', $this->createTimestamp('2013-10-09 08:07:06'), $request_time, $granularity_3), + array('2 months', $this->createTimestamp('2013-10-09 08:07:06'), $request_time, $granularity_4), + array('6 months', $this->createTimestamp('2013-06-09 10:09:08'), $request_time), + array('11 months', $this->createTimestamp('2013-01-11 07:09:08'), $request_time), array('11 months 4 weeks', $this->createTimestamp('2012-12-12 10:09:08'), $request_time), array('11 months 4 weeks 2 days', $this->createTimestamp('2012-12-12 10:09:08'), $request_time, $granularity_3), // Checks for years and possibly months, days, hours, minutes or seconds. array('1 year', $this->createTimestamp('2012-12-11 10:09:08'), $request_time), - array('1 year 1 minute', $this->createTimestamp('2012-12-11 10:08:08'), $request_time), - array('1 year 1 day', $this->createTimestamp('2012-12-10 10:09:08'), $request_time), + array('1 year', $this->createTimestamp('2012-12-11 10:08:08'), $request_time), + array('1 year', $this->createTimestamp('2012-12-10 10:09:08'), $request_time), array('2 years', $this->createTimestamp('2011-12-11 10:09:08'), $request_time), - array('2 years 2 minutes', $this->createTimestamp('2011-12-11 10:07:08'), $request_time), - array('2 years 2 days', $this->createTimestamp('2011-12-09 10:09:08'), $request_time), - array('2 years 2 months 2 days', $this->createTimestamp('2011-10-09 08:07:06'), $request_time, $granularity_3), - array('2 years 2 months 2 days 2 hours', $this->createTimestamp('2011-10-09 08:07:06'), $request_time, $granularity_4), + array('2 years', $this->createTimestamp('2011-12-11 10:07:08'), $request_time), + array('2 years', $this->createTimestamp('2011-12-09 10:09:08'), $request_time), + array('2 years 2 months', $this->createTimestamp('2011-10-09 08:07:06'), $request_time, $granularity_3), + array('2 years 2 months', $this->createTimestamp('2011-10-09 08:07:06'), $request_time, $granularity_4), array('10 years', $this->createTimestamp('2003-12-11 10:09:08'), $request_time), array('100 years', $this->createTimestamp('1913-12-11 10:09:08'), $request_time),