From 439109fb63641e148c955c782a2382f8d4403736 Mon Sep 17 00:00:00 2001 From: Christopher Walz Date: Fri, 30 Dec 2016 14:08:00 +0800 Subject: [PATCH] Move method to the DateUtil class, so it's globally accessible and plugins can also use relative times without copying the code. --- .../TimeModifierTemplatePlugin.class.php | 59 +------------------ .../install/files/lib/util/DateUtil.class.php | 57 ++++++++++++++++++ 2 files changed, 58 insertions(+), 58 deletions(-) diff --git a/wcfsetup/install/files/lib/system/template/plugin/TimeModifierTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/TimeModifierTemplatePlugin.class.php index 63533dd7e7..ea394a18e7 100644 --- a/wcfsetup/install/files/lib/system/template/plugin/TimeModifierTemplatePlugin.class.php +++ b/wcfsetup/install/files/lib/system/template/plugin/TimeModifierTemplatePlugin.class.php @@ -28,65 +28,8 @@ class TimeModifierTemplatePlugin implements IModifierTemplatePlugin { $date = DateUtil::format($dateTimeObject, DateUtil::DATE_FORMAT); $time = DateUtil::format($dateTimeObject, DateUtil::TIME_FORMAT); $isFutureDate = ($timestamp > TIME_NOW); - $dateTime = $this->getRelativeTime($dateTimeObject, $timestamp, $date, $time, $isFutureDate); + $dateTime = DateUtil::getRelativeTime($dateTimeObject, $timestamp, $date, $time, $isFutureDate); return ''; } - - /** - * Returns the relative date time identical to the relative time generated - * through JavaScript. - * - * @param \DateTime $dateTimeObject target date object - * @param integer $timestamp target timestamp - * @param string $date localized date - * @param string $time localized time - * @param boolean $isFutureDate true if timestamp is in the future - * @return string relative time - */ - protected function getRelativeTime(\DateTime $dateTimeObject, $timestamp, $date, $time, $isFutureDate) { - if ($isFutureDate) { - return str_replace('%time%', $time, str_replace('%date%', $date, WCF::getLanguage()->get('wcf.date.dateTimeFormat'))); - } - - // timestamp is less than 60 seconds ago - if ($timestamp >= TIME_NOW || TIME_NOW < ($timestamp + 60)) { - return WCF::getLanguage()->get('wcf.date.relative.now'); - } - // timestamp is less than 60 minutes ago (display 1 hour ago rather than 60 minutes ago) - else if (TIME_NOW < ($timestamp + 3540)) { - $minutes = max(round((TIME_NOW - $timestamp) / 60), 1); - - return WCF::getLanguage()->getDynamicVariable('wcf.date.relative.minutes', ['minutes' => $minutes]); - } - // timestamp is less than 24 hours ago - else if (TIME_NOW < ($timestamp + 86400)) { - $hours = round((TIME_NOW - $timestamp) / 3600); - - return WCF::getLanguage()->getDynamicVariable('wcf.date.relative.hours', ['hours' => $hours]); - } - // timestamp is less than 6 days ago - else if (TIME_NOW < ($timestamp + 518400)) { - $dtoNoTime = clone $dateTimeObject; - $dtoNoTime->setTime(0, 0, 0); - $currentDateTimeObject = DateUtil::getDateTimeByTimestamp(TIME_NOW); - $currentDateTimeObject->setTime(0, 0, 0); - - $days = $dtoNoTime->diff($currentDateTimeObject)->days; - $day = DateUtil::format($dateTimeObject, 'l'); - - return WCF::getLanguage()->getDynamicVariable('wcf.date.relative.pastDays', [ - 'days' => $days, - 'day' => $day, - 'time' => $time - ]); - } - - // timestamp is between ~700 million years BC and last week - $datetime = WCF::getLanguage()->get('wcf.date.shortDateTimeFormat'); - $datetime = str_replace('%date%', $date, $datetime); - $datetime = str_replace('%time%', $time, $datetime); - - return $datetime; - } } diff --git a/wcfsetup/install/files/lib/util/DateUtil.class.php b/wcfsetup/install/files/lib/util/DateUtil.class.php index c7d339f8d7..3010cff92d 100644 --- a/wcfsetup/install/files/lib/util/DateUtil.class.php +++ b/wcfsetup/install/files/lib/util/DateUtil.class.php @@ -480,6 +480,63 @@ final class DateUtil { $date->setISODate($year, 53, self::getFirstDayOfTheWeek()); return ($date->format('W') == 53 ? 53 : 52); } + + /** + * Returns the relative date time identical to the relative time generated + * through JavaScript. + * + * @param \DateTime $dateTimeObject target date object + * @param integer $timestamp target timestamp + * @param string $date localized date + * @param string $time localized time + * @param boolean $isFutureDate true if timestamp is in the future + * @return string relative time + */ + public static function getRelativeTime(\DateTime $dateTimeObject, $timestamp, $date, $time, $isFutureDate) { + if ($isFutureDate) { + return str_replace('%time%', $time, str_replace('%date%', $date, WCF::getLanguage()->get('wcf.date.dateTimeFormat'))); + } + + // timestamp is less than 60 seconds ago + if ($timestamp >= TIME_NOW || TIME_NOW < ($timestamp + 60)) { + return WCF::getLanguage()->get('wcf.date.relative.now'); + } + // timestamp is less than 60 minutes ago (display 1 hour ago rather than 60 minutes ago) + else if (TIME_NOW < ($timestamp + 3540)) { + $minutes = max(round((TIME_NOW - $timestamp) / 60), 1); + + return WCF::getLanguage()->getDynamicVariable('wcf.date.relative.minutes', ['minutes' => $minutes]); + } + // timestamp is less than 24 hours ago + else if (TIME_NOW < ($timestamp + 86400)) { + $hours = round((TIME_NOW - $timestamp) / 3600); + + return WCF::getLanguage()->getDynamicVariable('wcf.date.relative.hours', ['hours' => $hours]); + } + // timestamp is less than 6 days ago + else if (TIME_NOW < ($timestamp + 518400)) { + $dtoNoTime = clone $dateTimeObject; + $dtoNoTime->setTime(0, 0, 0); + $currentDateTimeObject = self::getDateTimeByTimestamp(TIME_NOW); + $currentDateTimeObject->setTime(0, 0, 0); + + $days = $dtoNoTime->diff($currentDateTimeObject)->days; + $day = self::format($dateTimeObject, 'l'); + + return WCF::getLanguage()->getDynamicVariable('wcf.date.relative.pastDays', [ + 'days' => $days, + 'day' => $day, + 'time' => $time + ]); + } + + // timestamp is between ~700 million years BC and last week + $datetime = WCF::getLanguage()->get('wcf.date.shortDateTimeFormat'); + $datetime = str_replace('%date%', $date, $datetime); + $datetime = str_replace('%time%', $time, $datetime); + + return $datetime; + } /** * Forbid creation of DateUtil objects. -- 2.20.1