mysql MONTH日期int到整月

时间:2013-12-20 09:35:57

标签: php html

我确定这很简单,但我无法解决这个问题。 我使用以下代码从mysql DB创建树网格。

$sql2 = mysql_query("SELECT DISTINCT YEAR(date) FROM blogs ORDER by YEAR(date) desc")or die(mysql_error());
$archive .= "<ul>";
while($row2 = mysql_fetch_assoc($sql2)){
    foreach ($row2 as $year) { 
    $archive .= "<li class=\"year\" id=\"$year\">$year<ul>";
        $sql3 = mysql_query("SELECT DISTINCT MONTH(date) FROM blogs WHERE YEAR(date)='$year' ORDER by MONTH(date) desc")or die(mysql_error());
        while($row3 = mysql_fetch_assoc($sql3)){
            foreach ($row3 as $month) { 
                $archive .= "<li id=\"$month\">$month<ul>";
                $sql4 = mysql_query("SELECT DISTINCT id,title FROM blogs WHERE MONTH(date)='$month' AND YEAR(date)='$year' ORDER by id desc")or die(mysql_error());
                while($row4 = mysql_fetch_assoc($sql4)){
                    $title2 = $row4['title'];
                    $bid = base64_encode($row4['id']);
                    $archive .= "<li><a href=\"../blog/?blogid=$bid\"> $title2 </a></li>";
                }
            } 
            $archive .= "</ul></li>";
        }
    $archive .= "</ul></li>";
    } 
}
$archive .= "</ul>";
echo $archive;

它很有效但是MONTH作为一个整数被回应,即如何将它转换为11月。

由于

2 个答案:

答案 0 :(得分:2)

您可以使用以下命令将数字转换为名称:

$timestamp = mktime(0, 0, 0, $month, 10);
$monthName = date("F", $timestamp);

示例:http://phpfiddle.org/main/code/x1u-5yy

答案 1 :(得分:0)

您也可以直接在sql上使用它。

select DATE_FORMAT(month,'%M') as month FROM blogs