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 8607021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:14:32+00:00 2026-06-12T03:14:32+00:00

I am new to php and I have a problem with time/date manipulation. I

  • 0

I am new to php and I have a problem with time/date manipulation.

I need to make statistics about daily/monthly/yearly visits in some store.
There is a mysql database with table “statistics” and fields: "statistic_id" ( integer, primary key ) , "visitors" ( integer ), and "dateAndTime" ( timestamp ). I have a form where user enter number of visitors that came to the store, and that number is inserted in database along with the time and date using now() function.

So basically my database looks like :

statistic_id , visitors , timeAndDate <br />
1............, 3........., 2012-09-29 14:45:02 <br />
2............, 5........., 2012-09-29 14:46:31 <br />
3............, 2........., 2012-09-29 18:48:11 ...etc.

What I need to do is sum and display all visitors that came in specific time interval.
09h-12h ; 12h-15h ; 15h-18h ; 18h-21h . So I need to display in table all these intervals and number of visitors for each of them. The problem is I don’t know how to extract these intervals and then get sum of visitors for them. I was trying everything I know and I could find, but without success. Also I will have to get monthly and yearly statistics, so how can I get all 12 months from this timeAndDate column and then sum all visitors for each month ?

Do anyone have an idea how to do this, and is willing to explain me in details please ? Thanks

  • 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-06-12T03:14:34+00:00Added an answer on June 12, 2026 at 3:14 am

    To get visitors between 09:00 and 12:00

    SELECT
        SUM(`visitors`)
    
    FROM
        `my_table`
    
    WHERE
        HOUR(`timeAndDate`) BETWEEN 9 AND 12
    

    To get visitors by month

    SELECT
        MONTH(`timeAndDate`),
        SUM(`visitors`)
    
    FROM
        `my_table`
    
    GROUP BY
        MONTH(`timeAndDate`)
    

    To get visitors by year

    SELECT
        YEAR(`timeAndDate`),
        SUM(`visitors`)
    
    FROM
        `my_table`
    
    GROUP BY
        YEAR(`timeAndDate`)
    

    Simple PHP example to output the months in a table

    <?php
    
        // Connect to database
        $db = mysqli_connect('localhost', 'root', 'root', 'test') or die('Could not connect to database');
    
        // Prepare sql question
        $sql = "SELECT
                    MONTHNAME(`timeAndDate`) AS `month`,
                    SUM(`visitors`) AS `visitors`
    
                FROM
                    `test`
    
                GROUP BY
                    MONTH(`timeAndDate`)";
    
        // Query the database
        $result = mysqli_query($db, $sql);
    
        // Begin table
        print '<table><thead><tr><th>Month</th><th>Visitors</th></tr></thead><tbody>';
    
        // Loop result
        while($row = mysqli_fetch_assoc($result)) {
            print "<tr><td>{$row['month']}</td><td>{$row['visitors']}</td></tr>";
        }
    
        // End table
        print '</tbody></table>';
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am new in php i have a problem in my php code.please help
Hi I am new to PHP and have a simple problem but I have
I am new to PHP and I have a problem in my code. I
I am fairly new to PHP OOP, the problem that I am have is
I'm new to PHP programming and have poor knowledge about it, but I want
New to PHP and MySQL, have heard amazing things about this website from Leo
I'm new to php and I have a problem. I'm working on a webpage
I have problem with date function in php. If I supply string in format
I'm quite new to php and have been reading Larry Ullman book to develop
I'm pretty new to PHP and have a String which contains an HTML page.

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.