从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 = ', ';
}