<?php
/**
* 取得修正後的時間戳(支援時區修正)
*/
function getFixedTime(int $timestamp = 0): int {
global $r_main; // 假設系統設定陣列
if ($timestamp === 0) $timestamp = time();
return $timestamp + ($r_main['timefix'] ?? 0);
}
/**
* 格式化為相對時間:剛剛、N分鐘前、N小時前、N天前、完整日期
*
* @param int $timestamp Unix 時間戳
* @return string 格式化後字串
*/
function formatRelativeTime(int $timestamp): string {
$now = time();
$diff = $now - getFixedTime($timestamp);
if ($diff < 0) $diff = 0; // 未來時間保護
if ($diff < 60) {
return $diff . '秒前';
} elseif ($diff < 3600) {
return floor($diff / 60) . '分鐘前';
} elseif ($diff < 86400) {
return floor($diff / 3600) . '小時前';
} elseif ($diff < 172800) { // 48小時內
return '1天前 ' . date('Y-n-j H:i', getFixedTime($timestamp));
} else {
return date('Y-n-j H:i', getFixedTime($timestamp));
}
}
?>
<?php
$timestamps = [
time() - 30, // 30秒前
time() - 300, // 5分鐘前
time() - 7200, // 2小時前
time() - 50000, // 13.8小時前
time() - 100000, // 1.15天前
strtotime('-5 days'), // 5天前
];
foreach ($timestamps as $ts) {
echo formatRelativeTime($ts) . "\n";
}
/*
30秒前
5分鐘前
2小時前
13小時前
1天前 2024-01-14 10:30
2024-01-10 10:30
*/
?>
<?php
class RelativeTimeFormatter {
private array $localeStrings = [
'zh_TW' => [
'just_now' => '剛剛',
'seconds_ago' => '{count}秒前',
'minutes_ago' => '{count}分鐘前',
'hours_ago' => '{count}小時前',
'yesterday' => '昨天 {time}',
'days_ago' => '{count}天前',
'date_format' => 'Y-m-d H:i',
],
'en_US' => [
'just_now' => 'just now',
'seconds_ago' => '{count} seconds ago',
'minutes_ago' => '{count} minutes ago',
'hours_ago' => '{count} hours ago',
'yesterday' => 'yesterday at {time}',
'days_ago' => '{count} days ago',
'date_format' => 'M j, Y g:i A',
],
];
public function __construct(private string $locale = 'zh_TW') {}
public function format(int $timestamp, int $now = 0): string {
$now = $now ?: time();
$diff = $now - $timestamp;
if ($diff < 0) {
return $this->formatFuture(-$diff);
}
$strings = $this->localeStrings[$this->locale] ?? $this->localeStrings['zh_TW'];
if ($diff < 5) return $strings['just_now'];
if ($diff < 60) return str_replace('{count}', (string)$diff, $strings['seconds_ago']);
if ($diff < 3600) return str_replace('{count}', (string)floor($diff/60), $strings['minutes_ago']);
if ($diff < 86400) return str_replace('{count}', (string)floor($diff/3600), $strings['hours_ago']);
if ($diff < 172800) return str_replace(['{count}', '{time}'], ['1', date($strings['date_format'], $timestamp)], $strings['yesterday']);
if ($diff < 2592000) return str_replace('{count}', (string)floor($diff/86400), $strings['days_ago']);
return date($strings['date_format'], $timestamp);
}
private function formatFuture(int $diff): string {
// 未來時間處理:倒數或顯示完整日期
if ($diff < 86400) return "即將發生";
return date($this->localeStrings[$this->locale]['date_format'], $timestamp);
}
}
// 使用
$formatter = new RelativeTimeFormatter('zh_TW');
echo $formatter->format(time() - 30); // 剛剛
?>
將時間戳輸出為 ISO 8601 格式,交由前端 JS 處理相對時間顯示,優點:
<time datetime="2024-01-15T10:30:00+08:00" class="timeago">2024-01-15 10:30</time>
<script src="timeago.min.js"></script>
<script>jQuery('.timeago').timeago();</script>
DATETIME 或 TIMESTAMP(UTC),避免時區混淆。UNIX_TIMESTAMP(created_at) 取得時間戳。相對時間格式化能大幅提升使用者體驗,後端實作簡單、前端庫成熟,根據專案架構選擇最適合的層級處理。