From 08d2a811b7155d9d82a1520a1afba0b605ed8144 Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Sun, 4 Jan 2026 15:58:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A6=E5=AE=A2=E4=BF=9D=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chukebao/controller/MomentsController.php | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/Server/application/chukebao/controller/MomentsController.php b/Server/application/chukebao/controller/MomentsController.php index fd3ae575..f0d6a4df 100644 --- a/Server/application/chukebao/controller/MomentsController.php +++ b/Server/application/chukebao/controller/MomentsController.php @@ -28,6 +28,12 @@ class MomentsController extends BaseController $labels = $this->request->param('labels', []); // 标签列表 $timingTime = $this->request->param('timingTime', date('Y-m-d H:i:s')); // 定时发布时间 $immediately = $this->request->param('immediately', false); // 是否立即发布 + + // 格式化时间字符串为统一格式 + $timingTime = $this->normalizeTimingTime($timingTime); + if ($timingTime === false) { + return ResponseHelper::error('定时发布时间格式不正确'); + } // 参数验证 if (empty($text) && empty($picUrlList) && empty($videoUrl)) { @@ -173,6 +179,12 @@ class MomentsController extends BaseController $labels = $this->request->param('labels', []); $timingTime = $this->request->param('timingTime', date('Y-m-d H:i:s')); $immediately = $this->request->param('immediately', false); + + // 格式化时间字符串为统一格式 + $timingTime = $this->normalizeTimingTime($timingTime); + if ($timingTime === false) { + return ResponseHelper::error('定时发布时间格式不正确'); + } // 读取待编辑记录 /** @var KfMoments|null $moments */ @@ -427,4 +439,66 @@ class MomentsController extends BaseController return ResponseHelper::error('删除失败:' . $e->getMessage()); } } + + /** + * 规范化时间字符串为 Y-m-d H:i:s 格式 + * 支持多种时间格式: + * - "2026年1月5日15:43:00" + * - "2026-01-05 15:43:00" + * - "2026/01/05 15:43:00" + * - 时间戳 + * @param string|int $timingTime 时间字符串或时间戳 + * @return string|false 格式化后的时间字符串,失败返回false + */ + private function normalizeTimingTime($timingTime) + { + if (empty($timingTime)) { + return date('Y-m-d H:i:s'); + } + + // 如果是时间戳 + if (is_numeric($timingTime) && strlen($timingTime) == 10) { + return date('Y-m-d H:i:s', $timingTime); + } + + // 如果是毫秒时间戳 + if (is_numeric($timingTime) && strlen($timingTime) == 13) { + return date('Y-m-d H:i:s', intval($timingTime / 1000)); + } + + // 如果已经是标准格式,直接返回 + if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $timingTime)) { + return $timingTime; + } + + // 处理中文日期格式:2026年1月5日15:43:00 或 2026年01月05日15:43:00 + if (preg_match('/^(\d{4})年(\d{1,2})月(\d{1,2})日(\d{1,2}):(\d{1,2}):(\d{1,2})$/', $timingTime, $matches)) { + $year = $matches[1]; + $month = str_pad($matches[2], 2, '0', STR_PAD_LEFT); + $day = str_pad($matches[3], 2, '0', STR_PAD_LEFT); + $hour = str_pad($matches[4], 2, '0', STR_PAD_LEFT); + $minute = str_pad($matches[5], 2, '0', STR_PAD_LEFT); + $second = str_pad($matches[6], 2, '0', STR_PAD_LEFT); + return "{$year}-{$month}-{$day} {$hour}:{$minute}:{$second}"; + } + + // 处理中文日期格式(无秒):2026年1月5日15:43 + if (preg_match('/^(\d{4})年(\d{1,2})月(\d{1,2})日(\d{1,2}):(\d{1,2})$/', $timingTime, $matches)) { + $year = $matches[1]; + $month = str_pad($matches[2], 2, '0', STR_PAD_LEFT); + $day = str_pad($matches[3], 2, '0', STR_PAD_LEFT); + $hour = str_pad($matches[4], 2, '0', STR_PAD_LEFT); + $minute = str_pad($matches[5], 2, '0', STR_PAD_LEFT); + return "{$year}-{$month}-{$day} {$hour}:{$minute}:00"; + } + + // 尝试使用 strtotime 解析其他格式 + $timestamp = strtotime($timingTime); + if ($timestamp !== false) { + return date('Y-m-d H:i:s', $timestamp); + } + + // 如果所有方法都失败,返回 false + return false; + } } \ No newline at end of file