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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:19:37+00:00 2026-05-27T09:19:37+00:00

I want to make a summary script, for example, how many new users are

  • 0

I want to make a summary script, for example, how many new users are there today, yesterday, this week, last week, this month, last month, so on.

I’m using CodeIgniter, so I wonder which one is better? Using multiple calls on models like this :

$this->model->get_today_users();
$this->model->get_yesterday_users();
$this->model->get_this_week_users();
// so on

Each one of the functions above only return the number of users with specific date range (e.g. SELECT COUNT(*) WHERE join_date=NOW() for today_users , etc – of course using CI active record ).

Or is it better having one single query with all necessary subqueries like this :

function stats(){
$this->db->select('*');
$this->db->select('(SELECT COUNT(*) FROM users WHERE join_date=NOW()) as today');
$this->db->select('(SELECT COUNT(*) FROM users WHERE join_date=NOW()-INTERVAL 1 DAY) as yesterday');
$this->db->select('(SELECT COUNT(*) FROM users WHERE WEEK(join_date)=WEEK(NOW()) ) as this_week');
$this->db->select('(SELECT COUNT(*) FROM users WHERE WEEK(join_date)=WEEK(NOW())-1 ) as last_week');
$this->db->select('(SELECT COUNT(*) FROM users WHERE MONTH(join_date)=MONTH(NOW()) ) as this_month');
$this->db->select('(SELECT COUNT(*) FROM users WHERE MONTH(join_date)=MONTH(NOW())-1 ) as last_month');
$this->db->from('users');
$ret = $this->db->get();
}

and then calling for example $data[‘stat’] = $this->model->stats(); so in views, I can call $stat->today, $stat->this_week, etc.

Thanks in advance for the answers.

UPDATE : What if I want to make a function that receives a date? Let’s say I have to loop a month like this:

//for the sake of simplicity
foreach(day in a month as $r){
    $this->model->get_user_on_this_day($r);
}

This means for a 30-day month, it will loop 30 times! Is there any better way than that?

  • 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-27T09:19:37+00:00Added an answer on May 27, 2026 at 9:19 am

    IMO, neither of the approach is optimized.
    Using least query is the best, so, you should do a GROUP BY query like below :

    select date_format(join_date, '%Y-%m-%d') as the_date, count(*) as total
    from users
    where join_date between $start AND $end   <-- the date range
    group by the_date;
    

    Once you have the result, which you can set an array using date as key,
    then you can further group by day, week, month…(whatever you want)


    Seems like OP don’t understand how this work :-

    The above query is doing a one-time query, and will return the rows like

    2011-12-09, 20 users join
    2011-12-08, 10 users join
    2011-12-07, 51 users join
    ...
    

    All you have to do is to iterate the results (not active record)

    $query = $this->db->query($sql, array($start, $end));
    $rtn   = array();
    $total = 0;
    foreach ($query->result() as $row)
    {
      $rtn[$row->the_date] = $row->total;
      $total += $row->total;
    }
    

    By the end of the loop, $total = total user registered from $start until $end.

    Is more effective and optimize then doing 30 (or 31) queries during the loop of month-day.

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

Sidebar

Related Questions

I want to make sure people can't type the name of a PHP script
I want to make a Configuration Data Manager. This would allow multiple services to
I want make datetimepicker in my project. Using jquery how it is possible? I
Let's say I want make some of my sources publicly available via my blog
I have style sheet with a class name changebackgroundcolor i want make change in
I am traversing a HTML document using javascript DOM. I want make a list
I want to make an etag that matches what Apache produces. How does apache
I want to make a table in SqlServer that will add, on insert, a
I want to make a copy of an ActiveRecord object, changing a single field
I want to make an entity that has an autogenerated primary key, but also

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.