// 在页面加载前立即设置主题,避免闪烁 (function() { const theme = localStorage.theme || (window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"); if (theme === "dark" || (!localStorage.theme && window.matchMedia("(prefers-color-scheme: dark)").matches)) { document.documentElement.classList.add("dark"); } else { document.documentElement.classList.remove("dark"); } })(); '; $this->need('header.php'); // 计算最近6个月的日期范围 $endDate = new DateTime(); $startDate = new DateTime(); $startDate->modify('-5 months'); // 最近6个月(包含当前月) $startDate->modify('first day of this month'); // 从当月第一天开始 // 只显示最近6个月 $db = Typecho_Db::get(); // 查询所有已发布的文章,在PHP中进行日期筛选 $posts = $db->fetchAll($db->select('title', 'created', 'slug', 'cid') ->from('table.contents') ->where('table.contents.type = ?', 'post') ->where('table.contents.status = ?', 'publish') ->order('table.contents.created', Typecho_Db::SORT_ASC)); // 组织热力图数据 $heatmapData = []; foreach ($posts as $post) { $postDate = date('Y-m-d', $post['created']); $postDateTime = new DateTime($postDate); // 只处理最近6个月的数据 if ($postDateTime >= $startDate && $postDateTime <= $endDate) { if (!isset($heatmapData[$postDate])) { $heatmapData[$postDate] = [ 'count' => 0, 'articles' => [] ]; } $heatmapData[$postDate]['count']++; // 构建正确的文章链接(添加.html后缀) $postUrl = Typecho_Common::url($post['slug'] . '.html', $this->options->index); $heatmapData[$postDate]['articles'][] = [ 'title' => $post['title'], 'url' => $postUrl ]; } } // 补全最近6个月中所有日期的数据 $currentDate = clone $startDate; $endOfRange = clone $endDate; $endOfRange->modify('last day of this month'); for ($date = clone $currentDate; $date <= $endOfRange; $date->modify('+1 day')) { $dateString = $date->format('Y-m-d'); if (!isset($heatmapData[$dateString])) { $heatmapData[$dateString] = [ 'count' => 0, 'articles' => [] ]; } } // 计算月份标签的准确位置 $monthPositions = []; $tempDate = clone $startDate; $firstDayOfWeek = $tempDate->format('N'); // 1=周一, 7=周日 $offset = $firstDayOfWeek - 1; // 转换为0=周一, 6=周日 $currentMonth = (int)$tempDate->format('n'); $monthPositions[$currentMonth] = 0; // 第一个月从位置0开始 $dayCount = 0; while ($tempDate <= $endOfRange) { $month = (int)$tempDate->format('n'); if ($month != $currentMonth) { $monthPositions[$month] = $dayCount; $currentMonth = $month; } $dayCount++; $tempDate->modify('+1 day'); } // 生成最近6个月的月份标签和位置 $monthLabels = []; $currentMonth = clone $startDate; for ($i = 0; $i < 6; $i++) { $monthNum = (int)$currentMonth->format('n'); $position = isset($monthPositions[$monthNum]) ? $monthPositions[$monthNum] : 0; $monthLabels[] = [ 'name' => $currentMonth->format('n月'), 'position' => $position ]; $currentMonth->modify('+1 month'); } // 计算总周数 $totalDays = $endOfRange->diff($startDate)->days + 1; $totalWeeks = ceil(($totalDays + $offset) / 7); //以下为往年今日文章调用代码 // 获取当前日期信息 $current_month = date('m'); $current_day = date('d'); $current_year = date('Y'); // 查询往年今日文章 $db = Typecho_Db::get(); $select = $db->select() ->from('table.contents') ->where('type = ?', 'post') ->where('status = ?', 'publish'); // 获取所有文章然后手动过滤 $all_posts = $db->fetchAll($select); $past_posts = array(); foreach ($all_posts as $post) { $post_month = date('m', $post['created']); $post_day = date('d', $post['created']); $post_year = date('Y', $post['created']); // 筛选条件:同月同日,但不是今年 if ($post_month == $current_month && $post_day == $current_day && $post_year != $current_year) { $past_posts[] = $post; } } // 按创建时间倒序排列 usort($past_posts, function($a, $b) { return $b['created'] - $a['created']; }); ?>
need("header.php"); ?> need('assets/lantern.html'); ?>