从mysql时间戳字段中选择不同的月份和年份,并在php中回显它们 [英] Select Distinct month and year from mysql timestamp field and echo them in php

查看:90
本文介绍了从mysql时间戳字段中选择不同的月份和年份,并在php中回显它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的mysql表有一个createdOn列,其文件类型为'timestamp',格式为2011-10-13 14:11:12.

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

我需要显示的是与createdOn列不同的月份,年份.

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

我已经搜索了stackover流,并能够使用以下代码回显几个月,

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 />';
}*

这将月份输出为:

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.

谢谢

推荐答案

使用此:

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

并且在您的查询中没有选择月份,只是:

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

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

它将是:

$comma = '';
while($row = mysqli_fetch_array($query)) {
    $date = $comma . date("F", mktime(0, 0, 0, $row['Month']));
    echo $comma . $date;
    $comma = ', ';
}

这篇关于从mysql时间戳字段中选择不同的月份和年份,并在php中回显它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆