Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6994255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:52:08+00:00 2026-05-27T19:52:08+00:00

I run a query against a MySQL database to get total number of visits

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:52:09+00:00Added an answer on May 27, 2026 at 7:52 pm

    Assumptions:

    • The date you want to display is the Monday Week Date for that week
    • Your main output is Week <week number>-<monday date of week>, Visits: <count>.
    • You know enough PHP to be able to pull the column Phrase

    The following SQL command should display for you the count of sessions per week of year, the week of year, and your desired phrase:

    SELECT COUNT(session) AS VisitCount
        , WEEKOFYEAR(timestamp) AS WeekOfYear_Value
        , MAKEDATE(
            CASE 
                WHEN WEEKOFYEAR(timestamp) = 52 
                    THEN YEAR(timestamp)-1 
                ELSE YEAR(timestamp) 
            END, (WEEKOFYEAR(timestamp) * 7)-4) AS DateOfWeek_Value
        , CONCAT('Week ', WEEKOFYEAR(timestamp)
            , '-', MAKEDATE(
                CASE 
                    WHEN WEEKOFYEAR(timestamp) = 52 
                        THEN YEAR(timestamp)-1 
                    ELSE YEAR(timestamp) 
                END, (WEEKOFYEAR(timestamp) * 7)-4) -- Monday
            , ', Visits: ' , COUNT(session), '.') AS Phrase
    FROM stats
    GROUP BY WEEKOFYEAR(timestamp)
    ORDER BY timestamp ASC
    

    Try it out on your existing data.

    [edit : additional code changes]

    To get you the Sunday output:

    SELECT COUNT(session) AS VisitCount
        , WEEKOFYEAR(timestamp) AS WeekOfYear_Value
        , CONCAT('Week ', WEEKOFYEAR(timestamp)
            , '-', MAKEDATE(
                CASE 
                    WHEN WEEKOFYEAR(timestamp) = 52 
                        THEN YEAR(timestamp)-1 
                    ELSE YEAR(timestamp) 
                END, 
                CASE 
                    WHEN WEEKOFYEAR(timestamp) = 52 
                        THEN (WEEKOFYEAR(timestamp) * 7)+3
                    ELSE (WEEKOFYEAR(timestamp) * 7)+2
                END
                )
        , ', Visits: ' , COUNT(session), '.') AS Phrase
    FROM stats
    GROUP BY WEEKOFYEAR(timestamp)
    ORDER BY timestamp ASC
    

    Also notice that this last update includes another CASE in the makedate command, to ensure consistency between dates from the current and last year values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to run a parameterized query against a DB2 database from .NET
I want to run an update query against a production database and as good
I have written a query I want to run against a MySQL db every
I have a sql query that I run against a sql server database eg.
I have a query which is run against a mssql database and I'm not
Here is the code I'm using: # Run the query against the database defined
I want to run an update query. The query will be run against multiple
using a c# asp.net page, building a SQL query string to run against an
I want to run MySQL query's on command without reloading the page. I think
If I run a query such as: SELECT COUNT(*) as num FROM table WHERE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.