Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the sample table structure I created:</p> <pre><code>-- SQL EXAMPLE CREATE TABLE IF NOT EXISTS `stack_test` ( `server` int(11) NOT NULL, `rtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `status` tinyint(4) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO `stack_test` (`server`, `rtime`, `status`) VALUES (1, '2012-01-01 11:00:24', 1), (1, '2012-01-01 11:02:24', 1), (1, '2012-01-01 11:05:24', 0), (2, '2012-01-01 11:10:24', 1), (1, '2012-01-01 11:30:24', 1); -- SQL EXAMPLE END </code></pre> <p>This is the PHP code:</p> <pre><code>&lt;?php $query = 'SELECT DISTINCT(`server`) `server` FROM stack_test'; $res = sql::exec($query); // replace with your function/method to execute SQL while ($row = mysql_fetch_assoc($res)) { $server = $row['server']; $uptimes = sql::exec('SELECT * FROM stack_test WHERE server=? ORDER BY rtime DESC',$server); $online = 0; $prev = time(); $prev = strtotime('2012-01-01 11:40:00'); // just to show that it works given the example while ($uptime = mysql_fetch_assoc($uptimes)) { if ($uptime['status'] == 1) { echo date('g:ia',$prev) . ' to ' . date('g:ia',strtotime($uptime['rtime'])) . ' = '.(($prev-strtotime($uptime['rtime']))/60).' mins&lt;br /&gt;'; $online += $prev-strtotime($uptime['rtime']); } $prev = strtotime($uptime['rtime']); } echo 'Server '.$server.' is up for '.($online/60).' mins.&lt;br /&gt;'; } ?&gt; </code></pre> <p>This is the output I get:</p> <pre><code>11:40am to 11:30am = 10 mins 11:05am to 11:02am = 3 mins 11:02am to 11:00am = 2 mins Server 1 is up for 15 mins. 11:40am to 11:10am = 30 mins Server 2 is up for 30 mins. </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload