https://www.mediawiki.org/wiki/Special:Code/MediaWiki/114067
Revision: 114067
Author: jeroendedauw
Date: 2012-03-17 20:58:46 +0000 (Sat, 17 Mar 2012)
Log Message:
-----------
adding a duration function to language that converts seconds to text
Modified Paths:
--------------
trunk/phase3/languages/Language.php
trunk/phase3/languages/messages/MessagesEn.php
Modified: trunk/phase3/languages/Language.php
===================================================================
--- trunk/phase3/languages/Language.php 2012-03-17 20:53:07 UTC (rev 114066)
+++ trunk/phase3/languages/Language.php 2012-03-17 20:58:46 UTC (rev 114067)
@@ -1910,6 +1910,44 @@
}
/**
+ * 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()
*
* @param $type String: can be 'date', 'time' or 'both'
Modified: trunk/phase3/languages/messages/MessagesEn.php
===================================================================
--- trunk/phase3/languages/messages/MessagesEn.php 2012-03-17 20:53:07 UTC
(rev 114066)
+++ trunk/phase3/languages/messages/MessagesEn.php 2012-03-17 20:58:46 UTC
(rev 114067)
@@ -4820,4 +4820,13 @@
'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}}',
+
);
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs