dtvpl739577 2012-05-12 19:35
浏览 20
已采纳

使用mysql和php只返回一次月份和日期

I am creating a archive of posts which I want to create archive links for when the articles were created in year / month format.

Created dates in database are stored in YYYY-MM-DD format and have written this so far.

$archive =
  mysql_query("SELECT DISTINCT DateCreated FROM blog ORDER by DateCreated")
  or die("Could not execute query");

while($row = mysql_fetch_array($archive) ){ 
  $Date = explode("-", $row["DateCreated"]);
  $Year = $Date[0];
  $Month = $Date[1];

  // Months of the year.
  $MonthName = array(
    "01" => "JAN",
    "02" => "FEB", 
    "03" => "MAR",
    "04" => "APR",
    "05" => "MAY",
    "06" => "JUN",
    "07" => "JUL",
    "08" => "AUG",
    "09" => "SEP",
    "10" => "OCT",
    "11" => "NOV",
    "12" => "DEC");    

 $archiveData .=
   "<a href='archive.php?".$Year."-".$Month.
   "'>".$MonthName[$Month]."-".$Year."</a><br />"; 
}

If I have these dates stored in my database

2012-04-07, 
2012-05-02, 
2012-05-13, 
2012-02-22, 

Then the above code produces links as

FEB-2012, 
APR-2012, 
MAY-2012, 
MAY-2012, 

It shows the months that have already been added, what I need to do is limit it to show it only once.

I think I need to change the query but not sure how to get the result I require any help would be appreciated.

  • 写回答

4条回答 默认 最新

  • dragon87836215 2012-05-12 19:39
    关注
    SELECT DISTINCT
      YEAR(DateCreated) AS yyyy,
      MONTH(DateCreated) AS mm
    FROM blog 
    ORDER BY DateCreated
    

    and accordingly

    $Year = $row["yyyy"];
    $Month  = $row["mm"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 C# TCP服务端,客户端退出后,不断有数据进来
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?