Move method to the DateUtil class, so it's globally accessible and plugins can also...
authorChristopher Walz <chris@cwalz.de>
Fri, 30 Dec 2016 06:08:00 +0000 (14:08 +0800)
committerChristopher Walz <chris@cwalz.de>
Fri, 30 Dec 2016 06:08:00 +0000 (14:08 +0800)
wcfsetup/install/files/lib/system/template/plugin/TimeModifierTemplatePlugin.class.php
wcfsetup/install/files/lib/util/DateUtil.class.php

index 63533dd7e7f1e87da9a17f4d3fb13faece11ff61..ea394a18e7f4c83dceabee3629755d97d6a6e7fe 100644 (file)
@@ -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 '<time datetime="'.DateUtil::format($dateTimeObject, 'c').'" class="datetime" data-timestamp="'.$timestamp.'" data-date="'.StringUtil::encodeHTML($date).'" data-time="'.StringUtil::encodeHTML($time).'" data-offset="'.$dateTimeObject->getOffset().'"'.($isFutureDate ? ' data-is-future-date="true"' : '').'>'.$dateTime.'</time>';
        }
-       
-       /**
-        * 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;
-       }
 }
index c7d339f8d7db552ecd644f0ec37a1036ae70a5bc..3010cff92d5206dea91b63144a00aee2509c617b 100644 (file)
@@ -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.