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

  • Home
  • SEARCH
  • 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 8810561
In Process

The Archive Base Latest Questions

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

I have the following function in my codeigniter app function set_day_peak($days, $offset, $network_id){ $days

  • 0

I have the following function in my codeigniter app

function set_day_peak($days, $offset, $network_id){

    $days = $days + $offset;
    $query = $this->db->query("SELECT MAX(total_online)AS total FROM network_traffic
    WHERE network_id = '$network_id' AND timestamp >= NOW() - INTERVAL $days DAY 
    AND timestamp <= NOW() - INTERVAL $offset DAY");
    $data = $query->row();

    return $data->total;

}   

The above function does return a value but its not accurate, what i need when i call

$this->set_day_peak(1,7,14)

is for it to return the max(total_online) from 00:01 to 23:59 on day 1 with a 7 day offset or (8 days ago).

As i am not brilliant with sql yet the only solution i could manage would be to use php to set the timestamps ie ($day_start and $day_end) then use SELECT MAX BETWEEN but i would like to see if it can be done with pure SQL Any ideas would be much appreciated.

  • 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-14T03:08:57+00:00Added an answer on June 14, 2026 at 3:08 am
    function set_day_peak($days, $offset, $network_id){
    
        $days = $days + $offset;
        $next = $days - 1;
        $query = $this->db->query("
           SELECT MAX(total_online) AS total
             FROM network_traffic
            WHERE network_id = '$network_id'
              AND timestamp >= DATE(NOW() - INTERVAL $days DAY) 
              AND timestamp <  DATE(NOW() - INTERVAL $next DAY)");
        $data = $query->row();
    
        return $data->total;
    }
    

    This formatting (whether legal or not) makes the SQL structure clear. The DATE function truncates the time value part of its argument expression. The comparison with a timestamp ‘adds zeros back’ for the time components. This should select the maximum of all values recorded for a given 24 hour period. The asymmetric conditions (‘>=’ vs ‘<‘) are important.

    An alternative formulation would be:

    function set_day_peak($days, $offset, $network_id){
    
        $days = $days + $offset;
        $query = $this->db->query("
           SELECT MAX(total_online) AS total
             FROM network_traffic
            WHERE network_id = '$network_id'
              AND DATE(timestamp) = DATE(NOW() - INTERVAL $days DAY)");
        $data = $query->row();
    
        return $data->total;
    }
    

    This may not be as efficient as it is much harder for the optimizer to use an index on timestamp because of the function call on timestamp. You should experiment.

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

Sidebar

Related Questions

I have following get_authors_list function in one of my Codeigniter model function get_authors_list($limit,$offset){ $data
I have the following codeigniter model function: function get_successful($project_id, $amount_backed){ $data = ''; $this->db->where('id',
Hi I've just started couple of days ago with codeigniter, I have a following
I have the following query SELECT * FROM (`user_profiles`) WHERE `user_id` = $user_id LIMIT
In my Codeigniter controller have the following private function which validates file uploads. private
In Codeigniter, I have the following model function get_item_history($id) { //from metadata_history get item_id
I have following function in one Activity public void AppExit() { Editor edit =
In my MVC application in Controller i have following function to add and focus
have the following function on my collection : getFiltered: function (status, city) { return
I have the following function set up to sequentially fade in divs (with class

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.