css - php根据时间顺序显示文章
天蓬老师
天蓬老师 2017-04-10 16:37:25
[PHP讨论组]

数据库里面时间字段存的是int型时间戳,想做出下面这个效果,没什么经验,希望大家能给个思路,谢谢!

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(5)
巴扎黑
sql = 'SELECT * FROM `table` ORDER BY created DESC';
//你的数据库驱动逻辑

//数据处理
if ($articles) {
    $res = [];
    foreach ($articles as $article) {
        $year = date('Y', $article['created']);
        $res[$year][] = $article; //以年为单位,作为数组索引,
    }
}

//最后输出结果类似这样
$res => [
    [2014] => [
        [0] => [
            //article 1
        ],
        [1] => [
            //article 2
        ]
    ],
    [2015] => [
        [0] => [
            //article 1
        ],
        [1] => [
            //article 2
        ]
    ]
]
PHP中文网
order by 时间字段名 DESC    //时间从大到小
order by 时间字段名 ASC     //时间从小到大
PHPz

你调用sql语句的时候order by field_time desc不就完了。

阿神

认为应该可以参考类似分页的解决方案

阿神

就是时间类型的转换嘛,用strtotime
$dated是你取出的时间戳
date("Y",strtotime($dated)); //年
date("m/d",strtotime($dated)); // 月/日

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号