帝国CMS模板中只获取当天发布的信息
2025-10-08
帝国 CMS 模板中,要调用当天发布的信息,只需通过 SQL 条件筛选出发布时间在当天范围内的内容即可。核心是计算当天的时间戳范围,再结合灵动标签实现。
方法:调用当天发布的信息
<?php
// 计算当天开始和结束的时间戳
$todayStart = strtotime(date('Y-m-d 00:00:00')); // 当天0点
$todayEnd = strtotime(date('Y-m-d 23:59:59')); // 当天23点59分
?>
[e:loop={"SELECT id,title,newstime,titleurl FROM phome_ecms_news
WHERE newstime BETWEEN <?=$todayStart?> AND <?=$todayEnd?> -- 限定当天时间范围
AND checked=1 -- 只调用已审核内容
ORDER BY newstime DESC -- 按发布时间倒序
LIMIT 10",10,24,0}] -- 调用10条
<li>
<a href="<?=$bqr['titleurl']?>" title="<?=$bqr['title']?>">
<?=esub($bqr['title'],30)?>
</a>
<span class="time"><?=date('H:i',$bqr['newstime'])?></span> <!-- 显示当天发布时间 -->
</li>
[/e:loop]
代码说明:
- 时间范围计算:
$todayStart
:当天 0 点 0 分 0 秒的时间戳$todayEnd
:当天 23 点 59 分 59 秒的时间戳BETWEEN <?=$todayStart?> AND <?=$todayEnd?>
:筛选发布时间在当天范围内的内容
- 适用场景:
- 首页 "今日最新" 板块
- 栏目页 "今日更新" 列表
- 侧边栏 "今日推荐" 等
不同模型的调用示例
1. 产品模型(当天发布的产品)
<?php
$todayStart = strtotime(date('Y-m-d 00:00:00'));
$todayEnd = strtotime(date('Y-m-d 23:59:59'));
?>
[e:loop={"SELECT id,title,newstime,titleurl FROM phome_ecms_product
WHERE newstime BETWEEN <?=$todayStart?> AND <?=$todayEnd?>
AND checked=1
ORDER BY newstime DESC
LIMIT 8",8,24,0}]
<div class="product-item">
<h3><?=$bqr['title']?></h3>
<p>发布时间:<?=date('Y-m-d H:i',$bqr['newstime'])?></p>
</div>
[/e:loop]
2. 带样式的今日资讯列表
php
<?php
$todayStart = strtotime(date('Y-m-d 00:00:00'));
$todayEnd = strtotime(date('Y-m-d 23:59:59'));
?>
<div class="today-news">
<h3>今日资讯</h3>
<ul>
[e:loop={"SELECT id,title,newstime,titleurl FROM phome_ecms_news
WHERE newstime BETWEEN <?=$todayStart?> AND <?=$todayEnd?>
AND checked=1
ORDER BY newstime DESC
LIMIT 5",5,24,0}]
<li>
<span class="dot"></span>
<a href="<?=$bqr['titleurl']?>"><?=esub($bqr['title'],25)?></a>
<span class="hour"><?=date('H:i',$bqr['newstime'])?></span>
</li>
[/e:loop]
</ul>
</div>
<style>
.today-news {
border: 1px solid #eee;
padding: 15px;
}
.today-news h3 {
margin: 0 0 10px;
color: #f00;
font-size: 16px;
}
.today-news ul {
margin: 0;
padding: 0;
list-style: none;
}
.today-news li {
margin: 8px 0;
padding-left: 15px;
position: relative;
}
.today-news .dot {
position: absolute;
left: 0;
top: 6px;
width: 6px;
height: 6px;
background: #f00;
border-radius: 50%;
}
.today-news .hour {
float: right;
color: #999;
font-size: 12px;
}
</style>
注意事项:
- 替换表名(
phome_ecms_news
)为实际使用的模型表名 newstime
是可修改的发布时间字段,若需按实际发布时间(不可修改)筛选,可替换为truetime
字段- 若当天没有发布内容,会显示空列表,可添加判断处理:
php<?php // 先查询是否有当天内容 global $empire, $dbtbpre; $todayCount = $empire->gettotal("SELECT id FROM {$dbtbpre}ecms_news WHERE newstime BETWEEN {$todayStart} AND {$todayEnd} AND checked=1"); ?> <?php if($todayCount > 0): ?> <!-- 输出灵动标签列表 --> <?php else: ?> <p>今日暂无相关内容</p> <?php endif; ?>
声明:本文来自用户分享和网络收集,仅供学习与参考,测试请备份。