Home > Backend Development > PHP Tutorial > css - php根据时间顺序显示文章

css - php根据时间顺序显示文章

WBOY
Release: 2016-06-06 20:20:44
Original
1362 people have browsed it

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

css - php根据时间顺序显示文章

回复内容:

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

css - php根据时间顺序显示文章

<code>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
        ]
    ]
]</code>
Copy after login

<code class="mysql">order by 时间字段名 DESC    //时间从大到小
order by 时间字段名 ASC     //时间从小到大</code>
Copy after login

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

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

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template