title: What is time format of $this->db->query_times array in Codeigniter?
body: <p>Surprisingly there is no documentation of it for both codeigniter v2 and v3. I just found out the code myself from a tutorial here is the <a href="http://sajanmaharjan.com.np/2013/02/05/code-igniter-log-all-queries/" rel="nofollow noreferrer">link</a>. The tutorial is not perfect and also not working but I tend to improve that. </p>
  
  <p>What I'm referring is <em>$this->db->query_times</em> and <em>$this->db->queries</em>. And I'm amazed to found out that these are working.</p>
  
  <p>Now you can use these in Codginiter hooks to determine which query is executed by using <em>$this->db->queries</em> and how much time it is taking by using <em>$this->db->query_times</em> and implement the hook after post controller</p>
  
  <p>The result of <em>$this->db->queries</em> in codeigniter v3 is </p>
  
  <pre><code>Array
  (
      [0] =&gt; SELECT GET_LOCK('0ae383feca87611f9ef779f13ae5d3f3', 300) AS ci_session_lock
      [1] =&gt; SELECT `data`
  FROM `ci_sessions_3`
  WHERE `id` = '9sh99dhtp1i51t21g498gerhem4e6gl6'
      [2] =&gt; SELECT * FROM `tradelog` WHERE status=1
  )
  </code></pre>
  
  <p>and the result of <em>$this->db->query_times</em> in codeigniter v3 is </p>
  
  <pre><code>Array
  (
      [0] =&gt; 0.00015497207641602
      [1] =&gt; 0.0003199577331543
      [2] =&gt; 0.0003359317779541
  )
  </code></pre>
  
  <p>Both array have same number of indexes because first array contains queries which is executed and second array contains these queries executed time.</p>
  
  <p>I have a little problem which is to determine the query_time format to properly convert into seconds or in minutes.</p>
  
  <h2>It'll be used like this</h2>
  
  <p>(<em>Ignore it if you're not interested how it will be used</em>)</p>
  
  <p>I'll use it to record all my queries in <em>queries.log.txt</em> file in development Environment by hooking <strong>LogQueryHook</strong> class after post controller hooks in codeigniter.</p>
  
  <p>Goto to <em>application/config/config.php</em> and change this</p>
  
  <pre><code>$config['enable_hooks'] = FALSE;
  </code></pre>
  
  <p>to this</p>
  
  <pre><code>switch (ENVIRONMENT)
  {
      case 'development':
          $config['enable_hooks'] = TRUE;
          break;
      case 'testing':
      case 'production':
      default:
          $config['enable_hooks'] = FALSE;
          break;
  }
  </code></pre>
  
  <p>so only in development you log all queries.</p>
  
  <p>Now goto <em>application/config/hooks.php</em> to hook your LogQueryHook class after post system . Just copy paste this code.</p>
  
  <pre><code>$hook['post_system'][] = array(
      'class' =&gt; 'LogQueryHook',
      'function' =&gt; 'log_queries',
      'filename' =&gt; 'LogQueryHook.php',
      'filepath' =&gt; 'hooks'
  );
  </code></pre>
  
  <p>Create a file name "LogQueryHook.php" in hooks folder in application folder.
  It'll look like this <em>application\hooks\LogQueryHook.php</em>
  Now paste this code to log every query with execution time took.</p>
  
  <pre><code>class LogQueryHook
  {
      function log_queries()
      {
          $CI = &amp;get_instance ();
          $times = $CI-&gt;db-&gt;query_times;
          $output = null;
          $queries = $CI-&gt;db-&gt;queries;
          $date_now = date('Y-m-d h:i:sa');
  
          /** Spaces are given for proper alignment of text */
          if (count ($queries) == 0) {
              $output .= $date_now . "        no queries\n";
          } else {
              foreach ($queries as $key =&gt; $query) {
                  $took = round (doubleval ($times[$key]), 3);
                  // I need to improve this line to record time properly
                  // and I don't know in which format codeginiter is giving me time either its is seconds or miliseconds.
                  $output .= $date_now . "        ===[took:{$took}]\n";
                  $query = str_replace (array("\r\n", "\r", "\n", "\\r", "\\n", "\\r\\n"), "\n                             ", "                             " . $query);
                  $output .= $query . "\n\n\n";
              }
          }
  
          $CI-&gt;load-&gt;helper ('file');
          if (!write_file (APPPATH . "/logs/queries.log.txt", $output, 'a+')) {
              log_message ('debug', 'Unable to write query the file');
          }
      }
  }
  </code></pre>
  
  <p>After that run any page in development environment and all the queries will be recorded in <em>application\logs\queries.log.txt</em> file.</p>