コード例 #1
0
ファイル: Abstract.php プロジェクト: redmexico/XoopsCore
 /**
  * Function to display formatted times in user timezone
  * Setting $timeoffset to null (by default) will skip timezone calculation for user, using default timezone instead, which is a MUST for cached contents
  *
  * @param        $time
  * @param string $format
  * @param string $timeoffset
  *
  * @return string
  */
 public static function formatTimestamp($time, $format = 'l', $timeoffset = null)
 {
     $xoops = Xoops::getInstance();
     $format_copy = $format;
     $format = strtolower($format);
     if ($format == 'rss' || $format == 'r') {
         $TIME_ZONE = '';
         if ($xoops->getConfig('server_TZ')) {
             $server_TZ = abs((int) ($xoops->getConfig('server_TZ') * 3600.0));
             $prefix = $xoops->getConfig('server_TZ') < 0 ? ' -' : ' +';
             $TIME_ZONE = $prefix . date('Hi', $server_TZ);
         }
         $date = gmdate('D, d M Y H:i:s', (int) $time) . $TIME_ZONE;
         return $date;
     }
     if (($format == 'elapse' || $format == 'e') && $time < time()) {
         $elapse = time() - $time;
         if ($days = floor($elapse / (24 * 3600))) {
             $num = $days > 1 ? sprintf(XoopsLocale::LF_AGO_DAYS, $days) : XoopsLocale::LF_AGO_ONE_DAY;
         } elseif ($hours = floor($elapse % (24 * 3600) / 3600)) {
             $num = $hours > 1 ? sprintf(XoopsLocale::LF_AGO_HOURS, $hours) : XoopsLocale::LF_AGO_ONE_HOUR;
         } elseif ($minutes = floor($elapse % 3600 / 60)) {
             $num = $minutes > 1 ? sprintf(XoopsLocale::LF_AGO_MINUTES, $minutes) : XoopsLocale::LF_AGO_ONE_MINUTE;
         } else {
             $seconds = $elapse % 60;
             $num = $seconds > 1 ? sprintf(XoopsLocale::LF_AGO_SECONDS, $seconds) : sprintf(XoopsLocale::LF_AGO_ONE_SECOND);
         }
         return $num;
     }
     // disable user timezone calculation and use default timezone,
     // for cache consideration
     if ($timeoffset === null) {
         $timeoffset = $xoops->getConfig('default_TZ') == '' ? '0.0' : $xoops->getConfig('default_TZ');
     }
     $usertimestamp = $xoops->getUserTimestamp($time, $timeoffset);
     switch ($format) {
         case 's':
             $datestring = self::getFormatShortDate();
             break;
         case 'm':
             $datestring = self::getFormatMediumDate();
             break;
         case 'mysql':
             $datestring = 'Y-m-d H:i:s';
             break;
         case 'l':
             $datestring = self::getFormatLongDate();
             break;
         case 'c':
         case 'custom':
             static $current_timestamp, $today_timestamp, $monthy_timestamp;
             if (!isset($current_timestamp)) {
                 $current_timestamp = $xoops->getUserTimestamp(time(), $timeoffset);
             }
             if (!isset($today_timestamp)) {
                 $today_timestamp = mktime(0, 0, 0, gmdate('m', $current_timestamp), gmdate('d', $current_timestamp), gmdate('Y', $current_timestamp));
             }
             if (abs($elapse_today = $usertimestamp - $today_timestamp) < 24 * 60 * 60) {
                 $datestring = $elapse_today > 0 ? XoopsLocale::getFormatToday() : XoopsLocale::getFormatYesterday();
             } else {
                 if (!isset($monthy_timestamp)) {
                     $monthy_timestamp[0] = mktime(0, 0, 0, 0, 0, gmdate('Y', $current_timestamp));
                     $monthy_timestamp[1] = mktime(0, 0, 0, 0, 0, gmdate('Y', $current_timestamp) + 1);
                 }
                 if ($usertimestamp >= $monthy_timestamp[0] && $usertimestamp < $monthy_timestamp[1]) {
                     $datestring = self::getFormatMonthDay();
                 } else {
                     $datestring = self::getFormatYearMonthDay();
                 }
             }
             break;
         default:
             if ($format != '') {
                 $datestring = $format_copy;
             } else {
                 $datestring = self::getFormatLongDate();
             }
             break;
     }
     return ucfirst(gmdate($datestring, $usertimestamp));
 }