沉默的博客 - 今日
type: channel
link: https://cmsblog.cn/tag/%E4%BB%8A%E6%97%A5/
items: 适用于Typecho的那年今日代码

适用于Typecho的那年今日代码
type: item
date: 2023-04-14T09:50:00+08:00
description: 开头可以在Typecho博客上展示当前文章日期在过去几年的今天所发的其他文章之前在一些博客上看到这个那年今日，感觉还不错，就借鉴了一下，并进行优化处理，且兼容了sqlite。代码将以下代码放入functions.phpfunction historyToday($created) { $date = date('m/d', $created); $date_m = date('m月', $created); $date_d = date('d日', $created); $time = time(); $db = Typecho_Db::get(); $prefix = $db->getPrefix(); $limit = 5;//显示多少篇文章 $adapter = $db->getAdapterName(); if ("Pdo_SQLite" === $adapter || "SQLite" === $adapter) { $sql = "SELECT * FROM `{$prefix}contents` WHERE strftime('%m-%d',datetime(datetime(created, 'unixepoch'))) = '{$date}' and created <= {$time} and created != {$created} and type = 'post' and status = 'publish' and (password is NULL or password = '') LIMIT ".$limit; } if ("Pdo_Mysql" === $adapter || "Mysql" === $adapter || "Mysqli" === $adapter) { $sql = "SELECT * FROM `{$prefix}contents` WHERE DATE_FORMAT(FROM_UNIXTIME(created), '%m/%d') = '{$date}' and created <= {$time} and created != {$created} and type = 'post' and status = 'publish' and (password is NULL or password = '') LIMIT ".$limit; } $result = $db->query($sql); $historyTodaylist = []; if ($result instanceof Traversable) { foreach ($result as $item) { $item = Typecho_Widget::widget('Widget_Abstract_Contents')->push($item); $title = htmlspecialchars($item['title']); $permalink = $item['permalink']; $date = date('Y年m月d日',$created); $historydate = date('Y年m月d日',$item['created']); $historyTodaylist[] = array( "title" => $title, "permalink" => $permalink, "date" => $historydate ); } } if (count($historyTodaylist) > 0){ echo " 那年今日 {$date_m} {$date_d} "; foreach ($historyTodaylist as $item){ echo " {$item['date']}{$item['title']} "; } echo " "; } }在文章页面合适的地方插入如下代码：<?php historyToday($this->created)?>这样就大功告成了!总结仅展示PHP部分，至于样式则可以自己写，这里就不过多讲述!