Implements dateDiff modifier
authorMatthias Schmidt <gravatronics@live.com>
Wed, 8 Aug 2012 18:04:46 +0000 (20:04 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Wed, 8 Aug 2012 18:04:46 +0000 (20:04 +0200)
Example: `{122331660|dateDiff:0:true}` results in `3 years, 10 months, 2 weeks, a day, 21 hours and a minute ago`, while `{122331660|dateDiff:0}` results in `3 years ago`.

wcfsetup/install/files/lib/system/template/plugin/DateDiffModifierTemplatePlugin.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/template/plugin/DatediffModifierTemplatePlugin.class.php [deleted file]
wcfsetup/install/files/lib/util/DateUtil.class.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

diff --git a/wcfsetup/install/files/lib/system/template/plugin/DateDiffModifierTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/DateDiffModifierTemplatePlugin.class.php
new file mode 100644 (file)
index 0000000..c3ac5dc
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+namespace wcf\system\template\plugin;
+use wcf\system\template\TemplateEngine;
+use wcf\util\DateUtil;
+
+/**
+ * The 'dateDiff' modifier calculates the difference between two unix timestamps
+ * and returns it as a textual date interval. The second parameter $fullInterval
+ * indicates if the full difference is returned or just a rounded difference.
+ * 
+ * Usage:
+ * {$timestamp|dateDiff}
+ * {"123456789"|dateDiff:$timestamp:$fullInverval}
+ *
+ * @author     Matthias Schmidt, Marcel Werk
+ * @copyright  2001-2012 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage system.template.plugin
+ * @category   Community Framework
+ */
+class DateDiffModifierTemplatePlugin implements IModifierTemplatePlugin {
+       /**
+        * @see wcf\system\template\IModifierTemplatePlugin::execute()
+        */
+       public function execute($tagArgs, TemplateEngine $tplObj) {
+               if (!isset($tagArgs[1])) {
+                       $tagArgs[1] = TIME_NOW;
+               }
+               
+               $fullInterval = false;
+               if (isset($tagArgs[2])) {
+                       $fullInterval = $tagArgs[2];
+               }
+               
+               $startTime = DateUtil::getDateTimeByTimestamp(min($tagArgs[0], $tagArgs[1]));
+               $endTime = DateUtil::getDateTimeByTimestamp(max($tagArgs[0], $tagArgs[1]));
+               
+               return DateUtil::formatInterval($endTime->diff($startTime), $fullInterval);
+       }
+}
diff --git a/wcfsetup/install/files/lib/system/template/plugin/DatediffModifierTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/DatediffModifierTemplatePlugin.class.php
deleted file mode 100644 (file)
index 289b62d..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-<?php
-namespace wcf\system\template\plugin;
-use wcf\system\template\TemplateEngine;
-use wcf\util\DateUtil;
-
-/**
- * The 'datediff' modifier calculates the difference between two unix timestamps.
- * 
- * Usage:
- * {$timestamp|datediff}
- * {"123456789"|datediff:$timestamp}
- *
- * @author     Marcel Werk
- * @copyright  2001-2011 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @package    com.woltlab.wcf
- * @subpackage system.template.plugin
- * @category   Community Framework
- */
-class DatediffModifierTemplatePlugin implements IModifierTemplatePlugin {
-       /**
-        * @see wcf\system\template\IModifierTemplatePlugin::execute()
-        */
-       public function execute($tagArgs, TemplateEngine $tplObj) {
-               // get timestamps
-               if (!isset($tagArgs[1])) $tagArgs[1] = TIME_NOW;
-               $start = min($tagArgs[0], $tagArgs[1]);
-               $end = max($tagArgs[0], $tagArgs[1]);
-               
-               // TODO: method doesn't exists anymore
-               // return DateUtil::diff($start, $end, 'string');
-               return '';
-       }
-}
index 081c5b2c7bf5219d50ce9e948a9bd8f7273c4c1e..2fd124dbf0c30ef95d9154dfcc3f71f3940e28a2 100644 (file)
@@ -137,6 +137,7 @@ final class DateUtil {
         * @param       string                          $format
         * @param       wcf\data\language\Language      $language
         * @param       wcf\data\user\User              $user
+        * @return      string
         */
        public static function format(\DateTime $time = null, $format = null, Language $language = null, User $user = null) {
                // get default values
@@ -157,6 +158,70 @@ final class DateUtil {
                return $output;
        }
        
+       /**
+        * Returns a formatted date interval. If $fullInterval is set true, the
+        * complete interval is returned, otherwise a rounded interval is used.
+        * 
+        * @param       \DateInterval   $interval
+        * @param       boolean         $fullInterval
+        * @return      string
+        */
+       public static function formatInterval(\DateInterval $interval, $fullInterval = false) {
+               $years = $interval->format('%y');
+               $months = $interval->format('%m');
+               $days = $interval->format('%d');
+               $weeks = floor($days / 7);
+               $hours = $interval->format('%h');
+               $minutes = $interval->format('%i');
+               
+               if ($fullInterval) {
+                       return WCF::getLanguage()->getDynamicVariable('wcf.date.interval.full', array(
+                               'days' => $days - 7 * $weeks,
+                               'firstElement' => $years ? 'years' : ($months ? 'months' : ($weeks ? 'weeks' : ($days ? 'days' : ($hours ? 'hours' : 'minutes')))),
+                               'hours' => $hours,
+                               'lastElement' => !$minutes ? (!$hours ? (!$days ? (!$weeks ? (!$months ? 'years' : 'months') : 'weeks') : 'days') : 'hours') : 'minutes',
+                               'minutes' => $minutes,
+                               'months' => $months,
+                               'weeks' => $weeks,
+                               'years' => $years
+                       ));
+               }
+               
+               if ($years) {
+                       return WCF::getLanguage()->getDynamicVariable('wcf.date.interval.years', array(
+                               'years' => $years
+                       ));
+               }
+               
+               if ($months) {
+                       return WCF::getLanguage()->getDynamicVariable('wcf.date.interval.months', array(
+                               'months' => $months
+                       ));
+               }
+               
+               if ($days) {
+                       if ($days > 7) {
+                               return WCF::getLanguage()->getDynamicVariable('wcf.date.interval.weeks', array(
+                                       'weeks' => $weeks
+                               ));
+                       }
+                       
+                       return WCF::getLanguage()->getDynamicVariable('wcf.date.interval.days', array(
+                               'days' => $days
+                       ));
+               }
+               
+               if ($hours) {
+                       return WCF::getLanguage()->getDynamicVariable('wcf.date.interval.hours', array(
+                               'hours' => $hours
+                       ));
+               }
+               
+               return WCF::getLanguage()->getDynamicVariable('wcf.date.interval.minutes', array(
+                       'minutes' => $minutes
+               ));
+       }
+       
        /**
         * Returns a localized date output.
         * 
index bb2f4634dc29f73dbe0fd887db434dfd70ea11be..3c90e59e4accd78c390a5cfafd0f8e174c13b669 100644 (file)
                <item name="wcf.date.timeFormat"><![CDATA[H:i]]></item>
                <item name="wcf.date.dateTimeFormat"><![CDATA[%date%, %time%]]></item>
                
+               <item name="wcf.date.interval.days"><![CDATA[Vor {if $days > 1}{#$days} Tagen{else}einem Tag{/if}]]></item>
+               <item name="wcf.date.interval.full"><![CDATA[Vor {if $years}{if $years > 1}{#$years} Jahren{else}einem Jahr{/if}{/if}{if $months}{if $firstElement != 'months'}{if $lastElement == 'months'} und {else}, {/if}{/if}{if $months > 1}{#$months} Monaten{else}einem Monat{/if}{/if}{if $weeks}{if $firstElement != 'weeks'}{if $lastElement == 'weeks'} und {else}, {/if}{/if}{if $weeks > 1}{#$weeks} Wochen{else}einer Woche{/if}{/if}{if $days}{if $firstElement != 'days'}{if $lastElement == 'days'} und {else}, {/if}{/if}{if $days > 1}{#$days} Tagen{else}einem Tag{/if}{/if}{if $hours}{if $firstElement != 'hours'}{if $lastElement == 'hours'} und {else}, {/if}{/if}{if $hours > 1}{#$hours} Stunden{else}einer Stunde{/if}{/if}{if $minutes}{if $firstElement != 'minutes' && $lastElement == 'minutes'} und {/if}{if $minutes > 1}{#$minutes} Minuten{else}einer Minute{/if}{/if}]]></item>
+               <item name="wcf.date.interval.hours"><![CDATA[Vor {if $hours > 1}{#$hours} Stunde{else}einer Stunde{/if}]]></item>
+               <item name="wcf.date.interval.minutes"><![CDATA[Vor {if $minutes > 1}{#$minutes} Minuten{else}einer Minute{/if}]]></item>
+               <item name="wcf.date.interval.months"><![CDATA[Vor {if $months > 1}{#$months} Monaten{else}einem Monat{/if}]]></item>
+               <item name="wcf.date.interval.weeks"><![CDATA[Vor {if $weeks > 1}{#$weeks} Wochen{else}einer Woche{/if}]]></item>
+               <item name="wcf.date.interval.years"><![CDATA[Vor {if $years > 1}{#$years} Jahren{else}einem Jahr{/if}]]></item>
+               
                <!-- variables for dynamic javascript datetime -->
                <item name="wcf.date.relative.minutes"><![CDATA['Vor ' + (($minutes > 1) ? $minutes + ' Minuten' : 'einer Minute' )]]></item>
                <item name="wcf.date.relative.hours"><![CDATA['Vor ' + (($hours > 1) ? $hours + ' Stunden' : 'einer Stunde' )]]></item>
index 2057381927fa943bc0e7dfa09c254a6bc720a859..709aaea469a56e7e2c9d20a46297a6b94a90021c 100644 (file)
                <item name="wcf.date.timeFormat"><![CDATA[g:ia]]></item>
                <item name="wcf.date.dateTimeFormat"><![CDATA[%date%, %time%]]></item>
                
+               <item name="wcf.date.interval.days"><![CDATA[{if $days > 1}{#$days} days{else}A day{/if} ago]]></item>
+               <item name="wcf.date.interval.full"><![CDATA[{if $years}{if $years > 1}{#$years} years{else}A year{/if}{/if}{if $months}{if $firstElement != 'months'}{if $firstElement != 'months'}{if $lastElement == 'months'} and {else}, {/if}{/if}{#$months} months{else}{if $firstElement == 'months'}A{else}a{/if} month{/if}{/if}{if $weeks}{if $firstElement != 'weeks'}{if $lastElement == 'weeks'} and {else}, {/if}{/if}{if $weeks > 1}{#$weeks} weeks{else}{if $firstElement == 'weeks'}A{else}a{/if} week{/if}{/if}{if $days}{if $firstElement != 'days'}{if $lastElement == 'days'} and {else}, {/if}{/if}{if $days > 1}{#$days} days{else}{if $firstElement == 'days'}A{else}a{/if} day{/if}{/if}{if $hours}{if $firstElement != 'hours'}{if $lastElement == 'hours'} and {else}, {/if}{/if}{if $hours > 1}{#$hours} hours{else}{if $firstElement == 'hours'}An{else}an{/if} hour{/if}{/if}{if $minutes}{if $firstElement != 'minutes' && $lastElement == 'minutes'} and {/if}{if $minutes > 1}{#$minutes} minutes{else}{if $firstElement == 'minutes'}A{else}a{/if} minute{/if}{/if} ago]]></item>
+               <item name="wcf.date.interval.hours"><![CDATA[{if $hours > 1}{#$hours} hours{else}An hour{/if} ago]]></item>
+               <item name="wcf.date.interval.minutes"><![CDATA[{if $minutes > 1}{#$minutes} minutes{else}A minute{/if} ago]]></item>
+               <item name="wcf.date.interval.months"><![CDATA[{if $months > 1}{#$months} months{else}A Month{/if} ago]]></item>
+               <item name="wcf.date.interval.weeks"><![CDATA[{if $weeks > 1}{#$weeks} weeks{else}A week{/if} ago]]></item>
+               <item name="wcf.date.interval.years"><![CDATA[{if $years > 1}{#$years} years{else}A year{/if} ago]]></item>
+               
                <!-- variables for dynamic javascript datetime -->
                <item name="wcf.date.relative.minutes"><![CDATA[(($minutes > 1) ? $minutes + ' minutes' : 'A minute') + ' ago']]></item>
                <item name="wcf.date.relative.hours"><![CDATA[(($hours > 1) ? $hours + ' hours' : 'An hour' ) + ' ago']]></item>