获取每个月的日期
1 现实成2012-12-1到2012-12-30日剩下的十一个月也是这样的显示 2 我先在实现了获取当前的日期2012-11-1,2012-11-2.....现在是这样的请问怎么实现第一种呢
代码如下<?php
/**
*
* 按时间归档插件
*
*/require_once(dirname(__FILE__)."/../include/common.inc.php");
$db = new mysqls($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,$cfg_dbname,$cfg_db_language);//实例化数据库类,本数据库类在本站前一篇日志
require_once(DEDEINC."/dedetag.class.php");
require_once(DEDEINC."/dedetemplate.class.php");
require_once(DEDEINC."/arc.searchview.class.php");
$doget = $_GET;
$page = new dz_page;
if(empty($doget['ac'])){
$tpl = new DedeTemplate('tpl');
$sql = "SELECT date(from_unixtime(pubdate)) AS d, count(*) AS c FROM `{$cfg_dbprefix}archives` GROUP BY date(from_unixtime(pubdate))";
$page = new dz_page($sql,50,"/date_");
$sql = $page->pagesql();
$tplfile = DEDETEMPLATE."/plus/date_index.htm";}elseif($doget['ac'] == "date"){
$tpl = new DedeTemplate('tpl');
$y = intval($doget['y']);
$m = intval($doget['m']);
$d = intval($doget['d']);
$f_date = "$y-$m-$d";
$sql = "SELECT arc.id,arc.pubdate,arc.senddate,arc.title,arc.title,arc.description,arc.typeid,t.namerule,t.typedir,t.typename FROM `{$cfg_dbprefix}archives` as arc left join {$cfg_dbprefix}arctype as t on arc.typeid = t.id where date(from_unixtime(pubdate)) = '$f_date' AND arc.arcrank = '0'";
$page = new dz_page($sql,3,"/{$f_date}_");
$sql = $page->pagesql();
$tplfile = DEDETEMPLATE."/plus/date_list.htm";
} $dsql->SetQuery($sql);
$dsql->Execute();
$arr_date = array();
while($row = $dsql->GetArray()){
$arr_date[] = $row;
}
if($doget['ac'] == "date"){
$l_c = count($arr_date);
for($i = 0;$i < $l_c;$i++){
$url = $arr_date[$i]["namerule"];
$url = str_replace('{typedir}',$arr_date[$i]["typedir"],$url);
$url = str_replace('{cmspath}',$cfg_cmspath,$url);
$f_y = date("Y",$arr_date[$i]["pubdate"]);
$f_m = date("m",$arr_date[$i]["pubdate"]);
$f_d = date("d",$arr_date[$i]["pubdate"]);
$url = str_replace('{Y}',$f_y,$url);
$url = str_replace('{D}',$f_d,$url);
$url = str_replace('{M}',$f_m,$url);
$url = str_replace('{timestamp}',$arr_date[$i]["senddate"],$url);
$arr_date[$i]["arcurl"] = str_replace('{aid}',$arr_date[$i]["id"],$url);
$arr_date[$i]["pubdate"] = date("Y-m-d H:i:s",$arr_date[$i]["pubdate"]);
}
}
$tpl->LoadTemplate($tplfile);
$pagehtml = $page -> show();
$tpl->Display();class dz_page{
private $url;
private $pagesize;
private $sql;
private $getpage;
//参数:sql语句,每页记录数,传递链接(例如"index.php?"或者"index.php?sort=1&"等)
function __construct($sql,$pagesize,$url) {
$this->url = $url;
$this->pagesize = $pagesize;
$this->sql = $sql;
$this->getpage = $_GET["page"];
if(!$this->getpage) {
$this->getpage=1;
}
}
//取记录总数
function getcount() {
return mysql_num_rows(mysql_query($this->sql));
}
//格式化sql语句
function pagesql() {
$nowpage = $this->getpage;
$limitNumber= $this->pagesize;
if($nowpage<1) {
$nowpage=1;
}
return $this->sql." limit ".($nowpage-1)*$limitNumber.",".$limitNumber;
} //显示分页,参数:显示数字链接个数,开始步进的页
function show($page = 5,$offset = 2){
$mpurl = $this->url;
$curr_page = $this->getpage;
$perpage = $this->pagesize;
$num = $this->getcount();//总记录数
$multipage = "";
if ($num > $perpage){
$pages = ceil($num / $perpage);
$from = $curr_page - $offset;
$to = $curr_page + $page - $offset - 1;
if ($page > $pages){
$from = 1;
$to = $pages;
}else{
if ($from < 1){
$to = $curr_page + 1-$from;
$from = 1;
if (($to - $from) < $page && ($to - $from) < $pages){
$to = $page;
}
}elseif ($to > $pages){
$from = $curr_page - $pages + $to;
$to = $pages;
if (($to - $from) < $page && ($to - $from) < $pages){
$from = $pages - $page + 1;
}
}
}
//使用跳转页的时候使用
/*$multipage .="
<script language=\"javascript\">
function gotoPage(value)
{
value = parseInt(value);
if(isNaN(value))
value = 0;
if(value<1)
value = 1;
if(value>".$pages.")
value = ".$pages.";
window.location.href = \"".$mpurl."page=\"+value;
}
</script>"; */
$multipage .= "<div id=\"dz_pages\">";
$multipage .= "<a class=\"p_total\"> 共 ".$num." 页 </a>\n";
$multipage .= "<a class=\"p_pages\"> ".$curr_page."/".$pages." </a>\n";
if ($curr_page - $offset > 1){
$multipage .= "<a class=\"p_redirect\" href=\"".$mpurl."page_1.html\" title='首页'>第一页</a>\n";
}
if ($curr_page != 1){
$multipage .= "<a class=\"p_next\" href=\"".$mpurl."page_".($curr_page-1).".html\" title='上一页'>上一页</a>\n";
}
for ($i = $from; $i <= $to; $i++){
if ($i != $curr_page){
$multipage .= "<a class=\"p_num\" href=\"".$mpurl."page_".$i.".html\" title='第".$i."页'>".$i."</a>\n";
}else{
$multipage .= "<a class=\"p_curpage\">".$i."</a>\n";
}
}
if ($curr_page != $pages){
$multipage .= "<a class=\"p_next\" href=\"".$mpurl."page_".($curr_page+1).".html\" title='下一页'>下一页</a>\n";
}
if ($curr_page + $offset < $pages){
$multipage .= "<a class=\"p_redirect\" href=\"".$mpurl."page_$pages.html\" title='尾页'>最后一页</a>\n";
}
//使用跳转页的时候使用
//$multipage .= "<input class=\"p_input\" title=\"输入页数,按回车跳转\" name=\"pageGo\" id=\"pageGo\" onKeydown=\"if(event.keyCode == 13) {gotoPage(this.value);return false;}\">\n";
$multipage .= "<a class=\"p_total\">".$perpage."条/页</a>\n";
$multipage .= "</div>";
}
return $multipage;
}
}
class mysqls{
private $link;
//在构造函数里连接数据库,不知道合理不,在config文件里实例化
//参数说明:地址,mysql用户名,密码,编码,数据库名,端口
function __construct($db_host,$db_username,$db_psd,$db_name,$db_charset,$port="3306") {
try{
if(!$this->link=@mysql_connect($db_host,$db_username,$db_psd,$port)) {
$error="<font color='red'>Error:数据库连接错误</font>";
throw new Exception($error);
}
if(!mysql_select_db($db_name,$this->link)) {
$error="<font color='red'>Error:选择数据库名错误</font>";
throw new Exception($error);
}
if($this->getversion()>'4.1') { //mysql版本大于4.1就设置编码,不是很清楚为什么要大于4.1版本的时候,参考别人的
if($db_charset) {
mysql_query("SET NAMES '{$db_charset}'");
}
if($this->getversion()>'5.0.1') { //mysql版本大于2.0.1就设置sqlmode为空,这个也是模仿别人的
mysql_query("SET sql_mode=''");
}
}
} catch (Exception $error) {
die($error->getMessage().":".mysql_error());
} }
//插入函数 $value是数组 例:array("name=>3,pwd=>2")
function insert($table,$value=array()) {
foreach($value as $feild => $val){
$feilds[]="`{$feild}`";
$vals[]="'{$val}'";
}
$sql="Insert INTO {$table} (".implode(",",$feilds).") value(".implode(",",$vals).")";
$this->query($sql);
}
//修改函数
function update($table,$value=array(),$where) {
foreach($value as $feild=>$val){
$set[]="{$feild}='{$val}'";
}
$sql="Update {$table} SET ".implode(",",$set)." Where ".$where;
$this->query($sql);
}
//删除函数
function del($table,$where) {
$sql="Delete FROM {$table} Where ".$where;
$this->query($sql);
}
//取记录数
function countnum($sql) {
return mysql_num_rows($this->query($sql));
}
//取mysql版本
function getversion() {
if($this->link) {
return mysql_get_server_info($this->link);
}
} //初始化query
function query($sql) {
$query=mysql_query($sql,$this->link) or die($this->showerror());
return $query;
}
//初始化fetch...
function fetch_array($result) {
return mysql_fetch_array($result);
}
//mysql报错
function showerror() {
echo"<font color='red'>数据库操作出错</font>:#".mysql_errno().":".mysql_error();
}
//关闭数据库
function close(){
mysql_close($this->link);
} }
?>
1 现实成2012-12-1到2012-12-30日剩下的十一个月也是这样的显示 2 我先在实现了获取当前的日期2012-11-1,2012-11-2.....现在是这样的请问怎么实现第一种呢
代码如下<?php
/**
*
* 按时间归档插件
*
*/require_once(dirname(__FILE__)."/../include/common.inc.php");
$db = new mysqls($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,$cfg_dbname,$cfg_db_language);//实例化数据库类,本数据库类在本站前一篇日志
require_once(DEDEINC."/dedetag.class.php");
require_once(DEDEINC."/dedetemplate.class.php");
require_once(DEDEINC."/arc.searchview.class.php");
$doget = $_GET;
$page = new dz_page;
if(empty($doget['ac'])){
$tpl = new DedeTemplate('tpl');
$sql = "SELECT date(from_unixtime(pubdate)) AS d, count(*) AS c FROM `{$cfg_dbprefix}archives` GROUP BY date(from_unixtime(pubdate))";
$page = new dz_page($sql,50,"/date_");
$sql = $page->pagesql();
$tplfile = DEDETEMPLATE."/plus/date_index.htm";}elseif($doget['ac'] == "date"){
$tpl = new DedeTemplate('tpl');
$y = intval($doget['y']);
$m = intval($doget['m']);
$d = intval($doget['d']);
$f_date = "$y-$m-$d";
$sql = "SELECT arc.id,arc.pubdate,arc.senddate,arc.title,arc.title,arc.description,arc.typeid,t.namerule,t.typedir,t.typename FROM `{$cfg_dbprefix}archives` as arc left join {$cfg_dbprefix}arctype as t on arc.typeid = t.id where date(from_unixtime(pubdate)) = '$f_date' AND arc.arcrank = '0'";
$page = new dz_page($sql,3,"/{$f_date}_");
$sql = $page->pagesql();
$tplfile = DEDETEMPLATE."/plus/date_list.htm";
} $dsql->SetQuery($sql);
$dsql->Execute();
$arr_date = array();
while($row = $dsql->GetArray()){
$arr_date[] = $row;
}
if($doget['ac'] == "date"){
$l_c = count($arr_date);
for($i = 0;$i < $l_c;$i++){
$url = $arr_date[$i]["namerule"];
$url = str_replace('{typedir}',$arr_date[$i]["typedir"],$url);
$url = str_replace('{cmspath}',$cfg_cmspath,$url);
$f_y = date("Y",$arr_date[$i]["pubdate"]);
$f_m = date("m",$arr_date[$i]["pubdate"]);
$f_d = date("d",$arr_date[$i]["pubdate"]);
$url = str_replace('{Y}',$f_y,$url);
$url = str_replace('{D}',$f_d,$url);
$url = str_replace('{M}',$f_m,$url);
$url = str_replace('{timestamp}',$arr_date[$i]["senddate"],$url);
$arr_date[$i]["arcurl"] = str_replace('{aid}',$arr_date[$i]["id"],$url);
$arr_date[$i]["pubdate"] = date("Y-m-d H:i:s",$arr_date[$i]["pubdate"]);
}
}
$tpl->LoadTemplate($tplfile);
$pagehtml = $page -> show();
$tpl->Display();class dz_page{
private $url;
private $pagesize;
private $sql;
private $getpage;
//参数:sql语句,每页记录数,传递链接(例如"index.php?"或者"index.php?sort=1&"等)
function __construct($sql,$pagesize,$url) {
$this->url = $url;
$this->pagesize = $pagesize;
$this->sql = $sql;
$this->getpage = $_GET["page"];
if(!$this->getpage) {
$this->getpage=1;
}
}
//取记录总数
function getcount() {
return mysql_num_rows(mysql_query($this->sql));
}
//格式化sql语句
function pagesql() {
$nowpage = $this->getpage;
$limitNumber= $this->pagesize;
if($nowpage<1) {
$nowpage=1;
}
return $this->sql." limit ".($nowpage-1)*$limitNumber.",".$limitNumber;
} //显示分页,参数:显示数字链接个数,开始步进的页
function show($page = 5,$offset = 2){
$mpurl = $this->url;
$curr_page = $this->getpage;
$perpage = $this->pagesize;
$num = $this->getcount();//总记录数
$multipage = "";
if ($num > $perpage){
$pages = ceil($num / $perpage);
$from = $curr_page - $offset;
$to = $curr_page + $page - $offset - 1;
if ($page > $pages){
$from = 1;
$to = $pages;
}else{
if ($from < 1){
$to = $curr_page + 1-$from;
$from = 1;
if (($to - $from) < $page && ($to - $from) < $pages){
$to = $page;
}
}elseif ($to > $pages){
$from = $curr_page - $pages + $to;
$to = $pages;
if (($to - $from) < $page && ($to - $from) < $pages){
$from = $pages - $page + 1;
}
}
}
//使用跳转页的时候使用
/*$multipage .="
<script language=\"javascript\">
function gotoPage(value)
{
value = parseInt(value);
if(isNaN(value))
value = 0;
if(value<1)
value = 1;
if(value>".$pages.")
value = ".$pages.";
window.location.href = \"".$mpurl."page=\"+value;
}
</script>"; */
$multipage .= "<div id=\"dz_pages\">";
$multipage .= "<a class=\"p_total\"> 共 ".$num." 页 </a>\n";
$multipage .= "<a class=\"p_pages\"> ".$curr_page."/".$pages." </a>\n";
if ($curr_page - $offset > 1){
$multipage .= "<a class=\"p_redirect\" href=\"".$mpurl."page_1.html\" title='首页'>第一页</a>\n";
}
if ($curr_page != 1){
$multipage .= "<a class=\"p_next\" href=\"".$mpurl."page_".($curr_page-1).".html\" title='上一页'>上一页</a>\n";
}
for ($i = $from; $i <= $to; $i++){
if ($i != $curr_page){
$multipage .= "<a class=\"p_num\" href=\"".$mpurl."page_".$i.".html\" title='第".$i."页'>".$i."</a>\n";
}else{
$multipage .= "<a class=\"p_curpage\">".$i."</a>\n";
}
}
if ($curr_page != $pages){
$multipage .= "<a class=\"p_next\" href=\"".$mpurl."page_".($curr_page+1).".html\" title='下一页'>下一页</a>\n";
}
if ($curr_page + $offset < $pages){
$multipage .= "<a class=\"p_redirect\" href=\"".$mpurl."page_$pages.html\" title='尾页'>最后一页</a>\n";
}
//使用跳转页的时候使用
//$multipage .= "<input class=\"p_input\" title=\"输入页数,按回车跳转\" name=\"pageGo\" id=\"pageGo\" onKeydown=\"if(event.keyCode == 13) {gotoPage(this.value);return false;}\">\n";
$multipage .= "<a class=\"p_total\">".$perpage."条/页</a>\n";
$multipage .= "</div>";
}
return $multipage;
}
}
class mysqls{
private $link;
//在构造函数里连接数据库,不知道合理不,在config文件里实例化
//参数说明:地址,mysql用户名,密码,编码,数据库名,端口
function __construct($db_host,$db_username,$db_psd,$db_name,$db_charset,$port="3306") {
try{
if(!$this->link=@mysql_connect($db_host,$db_username,$db_psd,$port)) {
$error="<font color='red'>Error:数据库连接错误</font>";
throw new Exception($error);
}
if(!mysql_select_db($db_name,$this->link)) {
$error="<font color='red'>Error:选择数据库名错误</font>";
throw new Exception($error);
}
if($this->getversion()>'4.1') { //mysql版本大于4.1就设置编码,不是很清楚为什么要大于4.1版本的时候,参考别人的
if($db_charset) {
mysql_query("SET NAMES '{$db_charset}'");
}
if($this->getversion()>'5.0.1') { //mysql版本大于2.0.1就设置sqlmode为空,这个也是模仿别人的
mysql_query("SET sql_mode=''");
}
}
} catch (Exception $error) {
die($error->getMessage().":".mysql_error());
} }
//插入函数 $value是数组 例:array("name=>3,pwd=>2")
function insert($table,$value=array()) {
foreach($value as $feild => $val){
$feilds[]="`{$feild}`";
$vals[]="'{$val}'";
}
$sql="Insert INTO {$table} (".implode(",",$feilds).") value(".implode(",",$vals).")";
$this->query($sql);
}
//修改函数
function update($table,$value=array(),$where) {
foreach($value as $feild=>$val){
$set[]="{$feild}='{$val}'";
}
$sql="Update {$table} SET ".implode(",",$set)." Where ".$where;
$this->query($sql);
}
//删除函数
function del($table,$where) {
$sql="Delete FROM {$table} Where ".$where;
$this->query($sql);
}
//取记录数
function countnum($sql) {
return mysql_num_rows($this->query($sql));
}
//取mysql版本
function getversion() {
if($this->link) {
return mysql_get_server_info($this->link);
}
} //初始化query
function query($sql) {
$query=mysql_query($sql,$this->link) or die($this->showerror());
return $query;
}
//初始化fetch...
function fetch_array($result) {
return mysql_fetch_array($result);
}
//mysql报错
function showerror() {
echo"<font color='red'>数据库操作出错</font>:#".mysql_errno().":".mysql_error();
}
//关闭数据库
function close(){
mysql_close($this->link);
} }
?>
<!--{dede:sql sql="SELECT date(from_unixtime(pubdate)) AS d, count(*) AS c FROM `dede_archives` GROUP BY date(from_unixtime(pubdate)) order by d desc"}-->
<a href="/[field:d /].html" style="pading-right:15px;line-height:22px;">[field:d /]</a>{/dede:sql}