mysql怎么表示不同年份,从mysql时间戳字段中选择不同的月份和年份,并在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.'
';

}*

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

解决方案

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值