So what I want to do is basically this:
Month and Year:
-All the data sent on that month and year
For example: February 2013:
-The posts on this date
March 2013:
-The posts on this date
and so on.
I have a date column on my table and it has the following format: day.month.year
I am using PHP and MySQL.
I have my own MVC framework that I'm using. I will simplify it to make it more understanble here. The function to fetch data:
function selectAll($sql){
$find = $this->prepare($sql);
$find->execute();
$fetchdata = $find->fetchAll(PDO::FETCH_ASSOC);
return $fetchdata;
}
And the code to get the dates :
$sql = "Select YEAR(Date) AS year, MONTH(Date) AS month, Headline from blog";
$data = $this->db->selectAll($sql);
foreach($data as $key => $value) {
$date[] = $value['month'].'.'.$value['year'];
}
$dateunique = array_unique($date);
This give me the results for the dates but I cannot figure out how to put the correct data under each date.
Use DATE_FORMAT
mysql function.
Example
SELECT DATE_FORMAT(date_field,'%M') as Month,DATE_FORMAT(date_field,'%Y') as Year FROM table_name
and then debug result of query
you will see two extra column for month
and year
then display it as your desire.
if date_field
is varchar
then use str_to_date
mysql function.
SELECT STR_TO_DATE(date_field,'%M') as Month, STR_TO_DATE(date_field,'%Y') as Year FROM table_name;
OP question update
Try GROUP_CONCAT
SELECT
*,
GROUP_CONCAT(created) AS month
FROM
table_name
GROUP BY MONTH(created) DESC;
$input
is %Y-%m
format // ex: 02-2013-> feb 2013 SELECT * FROM `table` WHERE DATE_FORMAT(str_to_date(`date_column`, '%d/%m/%Y'), '%Y-%m')= '".$input."';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.