Finding the first and last days of each month in the period of time
I have a task to make website article stats based on database results. I need to calculate the amount of articles was added monthly and pass these results to JavaScript stats function.
The first what I'm doing is finding the entire period in which articles have been added:
SELECT MIN(date) as startdate FROM articles LIMIT 1
SELECT MAX(date) as enddate FROM articles LIMIT 1
Now I have the period which starts with $startDate
and ends with $endDate
. These variables are unix timestamps as they are in the database. The column date
in database is also unix timestamp.
Next, I should find the amount of articles have been added in each month doing a query in foreach loop like:
SELECT COUNT(id) as total FROM article开发者_如何学JAVAs
WHERE date > ".$startMonth." AND date < ".$endMonth
But to make that I need to find the first and the last days of each month in the period. And I stucked at this moment. So please can you please suggest me how to find these first and last days as array. I think I should make something like strtotime("+1 month",$date)
in a while loop until $startDate
equals $endDate
. Thanks in advance.
Upd. There are total 46 articles in the database. What I am willing to have are results grouped by month:
SELECT date, COUNT(id) as total FROM `articles`
WHERE `author`=$author
GROUP BY date_format(date, '%Y-%m')
results: month posts 11-2010 13 12-2010 33
It seems that you can achieve this using a GROUP BY
, like
SELECT date_format(`date`, '%Y-%m'), COUNT(id) as total
FROM articles
GROUP BY date_format(`date`, '%Y-%m');
And you do not need the date boundaries ...
精彩评论