今日开发本站须要用到按时间归档文章的功能,即按文档发布时间将文章文类,以实现检索和统计功能,因而本身写了一个, 现分享给你们,相信你们工做和学习中有可能会用到,实现原理很简单,即取出文章发布时间戳的年月(年月日也能够,自 决定),生成相应的统计数据,调用时只须要知道该数组元素所标识时间的起始时间戳,便能检索出相应的数据。php
- /**
- * @desc 按时间归档函数实现
- * @author mengdejun
- * @param array $arrData 传入须要归档类所需参数
- */
- if(!function_exists("archives_document")){
- function archives_document($arrData){
- $rv=array();
- foreach($array as $index=>$value){
- $archivesTime=date("Y-m",$value['a_uptime']);
- $rv[$archivesTime][]=$value;
- //$rv[$archivesTime]+=1;
- }
- return $rv;
- }
- }
以上代码既能实现归档功能,不过须要注意的是传入数据必须包含须要归档函数的时间戳,至于命名什么的,本身看着办。 解析时须要用到php自带函数mktime来计算指定时间的起始时间戳。数组
- mktime(hour,minute,second,month,day,year,is_dst)
如何使用在这里就很少废话了,具体能够查看http://www.w3school.com.cn/php/func_date_mktime.asp以上面的函数为例,假设访问地址为:http://host/archives.php?time=2012-04ide
- $time=$_GET['time'];
- $arrTime=explode("-",$time);
- $btime = mktime(0,0,0,$arrTime[1],1,$arrTime[0]);//取出当前月第一天开始时间戳
- $etime = mktime(23,59,59,$arrTime[1],date("t"),$arrTime[0]);//取出当前月最后一天结束时间戳
- //后面的程序就你们自由发挥了,SQL查询啥的就不废话了。