dpoxk64080 2012-02-22 13:30
浏览 58
已采纳

从mysql timestamp字段中选择Distinct month和year,并在php中回显它们

My mysql table has a createdOn column with filedtype 'timestamp' in the format of 2011-10-13 14:11:12.

What I need is to show, is distinct month,year from createdOn column.

I have searched on stackover flow and was able to echo back months using following code,

*$sqlCommand = "SELECT DISTINCT MONTH(createdOn) AS 'Month' FROM videoBase ORDER BY createdOn DESC";
$query=mysqli_query($myConnection,$sqlCommand) or die(mysqli_error());
while($row = mysqli_fetch_array($query)) {
    $date = date("F", mktime(0, 0, 0, $row['Month']));
    echo ''.$date.' <br />';
}*

This outputs months as :

October
January

What I need is the output in the format of:

October 2011
January 2012

Can anybody please let me know, what changes I should make in the code in order to get the required output.

Thanks

  • 写回答

3条回答 默认 最新

  • dousui8263 2012-02-22 13:33
    关注

    Use this:

    $date = date("F Y", strtotime($row['Month']));
    

    and in your query don't select the month, just:

    SELECT DISTINCT createdOn AS 'Month' FROM videoBase ...
    

    So it will be:

    $comma = '';
    while($row = mysqli_fetch_array($query)) {
        $date = $comma . date("F", mktime(0, 0, 0, $row['Month']));
        echo $comma . $date;
        $comma = ', ';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决