fix: 文章列表及详情发布时间中英显示区分

This commit is contained in:
2025-06-19 16:26:10 +08:00
parent 6abe5be875
commit aceb29cacc
4 changed files with 69 additions and 8 deletions

View File

@@ -111,3 +111,70 @@ if (!function_exists('lang_i18n')) {
return Lang::get($lang_key, $vars, $lang); return Lang::get($lang_key, $vars, $lang);
} }
} }
if (!function_exists('date_format_i18n')) {
/**
* 格式化日期时间为指定语言
* @param int|string $datetime 要格式化的日期时间字符串
* @param string $lang 语言标识
* @param bool $keep_time 是否保留时间
* @return string
*/
function date_format_i18n(int|string $datetime, string $lang = '', bool $keep_time = false): string
{
// 处理语言标识,若未提供则使用当前语言设置,并转换为小写
$lang = $lang ?: Lang::getLangSet();
$lang = strtolower($lang);
// 处理日期时间输入
if (is_numeric($datetime)) {
// 若输入为数字,直接作为时间戳处理
$timestamp = (int)$datetime;
} elseif (is_string($datetime)) {
// 尝试将字符串转换为时间戳
$timestamp = strtotime($datetime);
if ($timestamp === false) {
return '';
}
} else {
return '';
}
// 根据$datetime传值决定格式
$date_parts = [];
if (is_numeric($datetime)) {
$date_str = date('Y-m-d', $timestamp);
$date_parts = explode('-', $date_str);
} elseif (is_string($datetime)) {
if (preg_match('/^\d{4}$/', $datetime)) {
$date_parts = [$datetime];
} elseif (preg_match('/^\d{4}-\d{1,2}$/', $datetime)) {
$date_parts = explode('-', $datetime);
} elseif (preg_match('/^\d{4}-\d{1,2}-\d{1,2}(.{1,9})?$/', $datetime)) {
$date_parts = explode('-', $datetime);
}
}
// 定义默认格式
$fmt = '';
// 根据语言和日期部分数量设置日期格式
$format = [
'zh-cn' => [0 => '', 1 => 'Y', 2 => 'Y-m', 3 => 'Y-m-d'],
'en-us' => [0 => '', 1 => 'Y', 2 => 'F Y', 3 => 'F j, Y'],
'default' => [0 => '', 1 => 'Y', 2 => 'Y-m', 3 => 'Y-m-d']
];
$fmt = $format[$lang][count($date_parts)] ?? $format['default'][count($date_parts)];
// 若需要保留时间
if ($keep_time) {
if ($lang === 'en-us') {
$fmt .= ', g:i A';
} else {
$fmt .= ' H:i:s';
}
}
return date($fmt, $timestamp);
}
}

View File

@@ -93,12 +93,6 @@ class Article extends Common
]) ])
->bypk($id) ->bypk($id)
->find(); ->find();
if (!$detail->isEmpty()) {
$detail->release_time = date('Y-m-d H:i:s', strtotime($detail->release_time));
if (cookie('think_lang') == 'en-us') {
$detail->release_time = 'Posted on ' . date('F j, Y', strtotime($detail->release_time));
}
}
View::assign('detail', $detail); View::assign('detail', $detail);
// 获取分享配置相关 // 获取分享配置相关

View File

@@ -21,7 +21,7 @@
<div class="ct"> <div class="ct">
<div class="blog_title"> <div class="blog_title">
<h2>{$detail.title}</h2> <h2>{$detail.title}</h2>
<p>{$detail.release_time}</p> <p>{$detail.release_time|date_format_i18n}</p>
</div> </div>
<!-- 文本渲染--> <!-- 文本渲染-->
<div class="blog_content">{$detail.content|raw}</div> <div class="blog_content">{$detail.content|raw}</div>

View File

@@ -36,7 +36,7 @@
<img src="{$ar.image}" /> <img src="{$ar.image}" />
<h3>{$ar.title}</h3> <h3>{$ar.title}</h3>
<p>{$ar.desc}</p> <p>{$ar.desc}</p>
<p>{$ar.release_time}</p> <p>{$ar.release_time|date_format_i18n}</p>
</a> </a>
</div> </div>
{/volist} {/volist}