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