From: Jeroen De Dauw Date: Tue, 20 Mar 2012 22:47:50 +0000 (+0000) Subject: handle cases where an empty array is provided by using this as default, also correctl... X-Git-Tag: 1.31.0-rc.0~24171 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/?a=commitdiff_plain;h=4bb8ef8a8246d96628eb2fec743dde539270b667;p=lhc%2Fweb%2Fwiklou.git handle cases where an empty array is provided by using this as default, also correctly handle cases where the smallest unit is not the second and the result is 0 smallestunit --- diff --git a/languages/Language.php b/languages/Language.php index facd39c9ae..1bad3fe7a8 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -1919,7 +1919,7 @@ class Language { * * @return string */ - public function formatDuration( $seconds, array $chosenIntervals = array( 'millennia', 'centuries', 'decades', 'years', 'days', 'hours', 'minutes', 'seconds' ) ) { + public function formatDuration( $seconds, array $chosenIntervals = array() ) { $intervals = array( 'millennia' => 1000 * 31557600, 'centuries' => 100 * 31557600, @@ -1932,16 +1932,20 @@ class Language { 'seconds' => 1, ); - if ( !empty( $chosenIntervals ) ) { - $intervals = array_intersect_key( $intervals, array_flip( $chosenIntervals ) ); + if ( empty( $chosenIntervals ) ) { + $chosenIntervals = array( 'millennia', 'centuries', 'decades', 'years', 'days', 'hours', 'minutes', 'seconds' ); } + $intervals = array_intersect_key( $intervals, array_flip( $chosenIntervals ) ); + $sortedNames = array_keys( $intervals ); + $smallestInterval = array_pop( $sortedNames ); + $segments = array(); foreach ( $intervals as $name => $length ) { $value = floor( $seconds / $length ); - if ( $value > 0 || ( $name == 'seconds' && empty( $segments ) ) ) { + if ( $value > 0 || ( $name == $smallestInterval && empty( $segments ) ) ) { $seconds -= $value * $length; $message = new Message( 'duration-' . $name, array( $value ) ); $segments[] = $message->inLanguage( $this )->escaped();