I run a query against a MySQL database to get total number of visits grouped by week.
The structure of the table is:
|ID (int) | SESSION (char) | TIMESTAMP (datetime) |
My query looks like this:
'SELECT COUNT(session), WEEKOFYEAR(timestamp)
FROM stats
GROUP BY WEEKOFYEAR(timestamp)
ORDER BY timestamp ASC';
So I get an array of week numbers eg 40,41,45 etc, along with the view count.
How can I convert this into readable date (using PHP)? It’s not practical to say, “Week number 40-something: 2000 visits”.
I’m looking for something in the form of StartDate-EndDate/Month/Year, eg. Week 5-11/12/2011, Visits: XXXX.
Any functions you might have used before? I’ve tried but with no luck.
Is my SQL query a correct way to do it? Assuming that I’m querying the db correctly for what I’m trying to accomplish, is there a way to convert a week number into a day/date span?
I’m also wondering what will happen if a week spans between two months or even two years.
Assumptions:
Week <week number>-<monday date of week>, Visits: <count>.PhraseThe following SQL command should display for you the count of sessions per week of year, the week of year, and your desired phrase:
Try it out on your existing data.
[edit : additional code changes]
To get you the Sunday output:
Also notice that this last update includes another
CASEin themakedatecommand, to ensure consistency between dates from the current and last year values.