From: Jeroen De Dauw Date: Sat, 17 Mar 2012 20:58:46 +0000 (+0000) Subject: adding a duration function to language that converts seconds to text X-Git-Tag: 1.31.0-rc.0~24225 X-Git-Url: http://git.cyclocoop.org/%22.%24image2.%22?a=commitdiff_plain;h=9d4049a0ce017e7815c09123e94b718dd19f903b;p=lhc%2Fweb%2Fwiklou.git adding a duration function to language that converts seconds to text --- diff --git a/languages/Language.php b/languages/Language.php index 854a9f1010..4e09501b9d 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -1909,6 +1909,44 @@ class Language { return $this->sprintfDate( $df, $ts ); } + /** + * Takes a number of seconds and turns it into a text using values such as hours and minutes. + * + * @since 1.20 + * + * @param integer $seconds The amount of seconds. + * @param array $chosenIntervals The intervals to enable. + * + * @return string + */ + public function duration( $seconds, array $chosenIntervals = array( 'years', 'days', 'hours', 'minutes', 'seconds' ) ) { + $intervals = array( + 'years' => 31557600, // 86400 * 365.25 + 'weeks' => 604800, + 'days' => 86400, + 'hours' => 3600, + 'minutes' => 60, + 'seconds' => 1, + ); + + if ( !empty( $chosenIntervals ) ) { + $intervals = array_intersect_key( $intervals, array_flip( $chosenIntervals ) ); + } + + $segments = array(); + + foreach ( $intervals as $name => $length ) { + $value = floor( $seconds / $length ); + + if ( $value > 0 || ( $name == 'seconds' && empty( $segments ) ) ) { + $seconds -= $value * $length; + $segments[] = wfMsgExt( 'duration-' . $name, 'parsemag', $value ); + } + } + + return $this->listToText( $segments ); + } + /** * Internal helper function for userDate(), userTime() and userTimeAndDate() * diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 39dcb3f405..043716b0e1 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -4820,4 +4820,13 @@ Otherwise, you can use the easy form below. Your comment will be added to the pa 'api-error-uploaddisabled' => 'Uploading is disabled on this wiki.', 'api-error-verification-error' => 'This file might be corrupt, or have the wrong extension.', +# Durations +'duration-seconds' => '$1 {{PLURAL:$1|second|seconds}}', +'duration-minutes' => '$1 {{PLURAL:$1|minute|minutes}}', +'duration-hours' => '$1 {{PLURAL:$1|hour|hours}}', +'duration-days' => '$1 {{PLURAL:$1|day|days}}', +'duration-weeks' => '$1 {{PLURAL:$1|week|weeks}}', +'duration-years' => '$1 {{PLURAL:$1|year|years}}', +'duration-centuries' => '$1 {{PLURAL:$1|century|centuries}}', + );