From: Matthias Schmidt Date: Wed, 8 Aug 2012 18:04:46 +0000 (+0200) Subject: Implements dateDiff modifier X-Git-Tag: 2.0.0_Beta_1~907^2^2~4 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=8ee81f2facc4bac81dadbb2be1b90fd8f082152e;p=GitHub%2FWoltLab%2FWCF.git Implements dateDiff modifier 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`. --- 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 index 0000000000..c3ac5dc9b5 --- /dev/null +++ b/wcfsetup/install/files/lib/system/template/plugin/DateDiffModifierTemplatePlugin.class.php @@ -0,0 +1,41 @@ + + * @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 index 289b62d6a2..0000000000 --- a/wcfsetup/install/files/lib/system/template/plugin/DatediffModifierTemplatePlugin.class.php +++ /dev/null @@ -1,34 +0,0 @@ - - * @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 ''; - } -} diff --git a/wcfsetup/install/files/lib/util/DateUtil.class.php b/wcfsetup/install/files/lib/util/DateUtil.class.php index 081c5b2c7b..2fd124dbf0 100644 --- a/wcfsetup/install/files/lib/util/DateUtil.class.php +++ b/wcfsetup/install/files/lib/util/DateUtil.class.php @@ -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. * diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index bb2f4634dc..3c90e59e4a 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -452,6 +452,14 @@ + 1}{#$days} Tagen{else}einem Tag{/if}]]> + 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}]]> + 1}{#$hours} Stunde{else}einer Stunde{/if}]]> + 1}{#$minutes} Minuten{else}einer Minute{/if}]]> + 1}{#$months} Monaten{else}einem Monat{/if}]]> + 1}{#$weeks} Wochen{else}einer Woche{/if}]]> + 1}{#$years} Jahren{else}einem Jahr{/if}]]> + 1) ? $minutes + ' Minuten' : 'einer Minute' )]]> 1) ? $hours + ' Stunden' : 'einer Stunde' )]]> diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 2057381927..709aaea469 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -452,6 +452,14 @@ + 1}{#$days} days{else}A day{/if} ago]]> + 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]]> + 1}{#$hours} hours{else}An hour{/if} ago]]> + 1}{#$minutes} minutes{else}A minute{/if} ago]]> + 1}{#$months} months{else}A Month{/if} ago]]> + 1}{#$weeks} weeks{else}A week{/if} ago]]> + 1}{#$years} years{else}A year{/if} ago]]> + 1) ? $minutes + ' minutes' : 'A minute') + ' ago']]> 1) ? $hours + ' hours' : 'An hour' ) + ' ago']]>