adding a duration function to language that converts seconds to text
authorJeroen De Dauw <jeroendedauw@users.mediawiki.org>
Sat, 17 Mar 2012 20:58:46 +0000 (20:58 +0000)
committerJeroen De Dauw <jeroendedauw@users.mediawiki.org>
Sat, 17 Mar 2012 20:58:46 +0000 (20:58 +0000)
languages/Language.php
languages/messages/MessagesEn.php

index 854a9f1..4e09501 100644 (file)
@@ -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()
         *
index 39dcb3f..043716b 100644 (file)
@@ -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}}',
+
 );