From 96d693af6414dc69fd6ca5aec2c9210357b1e2fe Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Thu, 19 Jun 2025 16:26:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=96=87=E7=AB=A0=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E5=8F=8A=E8=AF=A6=E6=83=85=E5=8F=91=E5=B8=83=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E4=B8=AD=E8=8B=B1=E6=98=BE=E7=A4=BA=E5=8C=BA=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/index/common.php | 67 +++++++++++++++++++++++++++ app/index/controller/Article.php | 6 --- app/index/view/pc/article/detail.html | 2 +- app/index/view/pc/article/index.html | 2 +- 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/app/index/common.php b/app/index/common.php index 7e901805..226b3689 100644 --- a/app/index/common.php +++ b/app/index/common.php @@ -111,3 +111,70 @@ if (!function_exists('lang_i18n')) { 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); + } +} diff --git a/app/index/controller/Article.php b/app/index/controller/Article.php index 9b1deb44..46393073 100644 --- a/app/index/controller/Article.php +++ b/app/index/controller/Article.php @@ -93,12 +93,6 @@ class Article extends Common ]) ->bypk($id) ->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); // 获取分享配置相关 diff --git a/app/index/view/pc/article/detail.html b/app/index/view/pc/article/detail.html index 007ca723..1be36565 100644 --- a/app/index/view/pc/article/detail.html +++ b/app/index/view/pc/article/detail.html @@ -21,7 +21,7 @@

{$detail.title}

-

{$detail.release_time}

+

{$detail.release_time|date_format_i18n}

{$detail.content|raw}
diff --git a/app/index/view/pc/article/index.html b/app/index/view/pc/article/index.html index 0760a28a..916810d5 100644 --- a/app/index/view/pc/article/index.html +++ b/app/index/view/pc/article/index.html @@ -36,7 +36,7 @@

{$ar.title}

{$ar.desc}

-

{$ar.release_time}

+

{$ar.release_time|date_format_i18n}

{/volist}