0 | 2008-06-10 15:30:01 | A
           1 | 2008-06-10 15:40:01 | A  // Calcute timestamp as 10 min
           1 | 2008-06-10 15:50:01 |A //Skip
           1 | 2008-06-10 16:00:01 |A  //Skip
           1 | 2008-06-10 16:10:01 |A   //Skip
           0 | 2008-06-10 16:20:01 |A   // Calcute timestamp as 10 min
           1 | 2008-06-10 16:30:01 |A  
1 | 2008-06-10 16:40:01 |A //Skip 1 | 2008-06-10 16:50:01 |A //Skip 1 | 2008-06-10 17:00:01 |A //Skip 0 | 2008-06-10 17:10:01 |A
1 | 2008-06-10 17:20:01 |A //Calcute difference as 10 min 1 | 2008-06-10 17:30:01 |A
0 | 2008-06-10 17:40:01 |A 0 | 2008-06-10 17:50:01 |A //Clauculate as 20 min

and how to push into array as it shows like this in the table

The fields are Status, imestamp and Name.
As in the above example
It should display as

In the table the

Name| Start Time | End Time | Difference

A | 2008-06-10 15:30:01 |2008-06-10 15:40:01 | 10
A | 2008-06-10 16:10:01 | 2008-06-10 16:20:01 | 10

Based on the status 0 timestamp difference has to be calculated.

Please help me.

    Here is the sample code. The end time of previous row is taken as the start time of the next row.

    The start time of next row should take only when status change.
    When the status changes from 0 to 1 only then the calculation of the time should happen

    <?php
    $str = "|master           | 2008-03-24 09:10:02 |      0 |
    | cluster      | 2008-03-24 09:20:01 |      0 |
    | master| 2008-03-24 09:20:01 |      1 |
    | cluster              | 2008-03-24 09:30:01 |      0 |
    | master           | 2008-03-24 09:30:01 |      1 |
    | cluster              | 2008-03-24 09:40:01 |      0 |
    | cluster              | 2008-03-24 09:50:02 |      0 |
    | cluster              | 2008-03-24 10:00:01 |      1 |
    | cluster              | 2008-03-24 10:10:01 |      0 |
    | cluster              | 2008-03-24 10:20:01 |      0 |
    | cluster              | 2008-03-24 10:30:01 |      0 |
    | cluster              | 2008-03-24 10:40:01 |      1 |
    | cluster              | 2008-03-24 10:50:01 |      0 |
    | cluster              | 2008-03-24 11:00:01 |      0 |
    | cluster              | 2008-03-24 11:10:01 |      0 |
    | cluster              | 2008-03-24 11:20:02 |      0 |
    | cluster              | 2008-03-24 11:30:02 |      1 |
    | cluster              | 2008-03-24 11:40:01 |      0 |
    | cluster              | 2008-03-24 11:50:02 |      0 |
    | cluster              | 2008-03-24 12:00:01 |      0 |
    | master               | 2008-03-24 12:00:01 |      0 |
    | cluster              | 2008-03-24 12:10:01 |      0 |
    | master               | 2008-03-24 12:10:01 |      0|
    | cluster              | 2008-03-24 12:20:01 |      0 |
    | master               | 2008-03-24 12:20:01 |      1 |";
    
    /****************************************************
    * clean up the posted data and store in array
    *****************************************************/
    $arr = explode("\n",$str);
    $data = array();
    foreach ($arr as $line)
    {
        $line = trim($line, ' |');
        $tmp = explode ('|', $line);
        foreach ($tmp as $k=>$v) $tmp[$k] = trim($v);
        $data[] = $tmp;
    }
    
    $records=array();
    $prevStatus = '';
    $storedTime = '';
    //echo '<pre>';
    foreach ($data as $row)
    {
        if ($row[0] != 'master') continue;
        if ($row[2] != $prevStatus)
        {
            if ($prevStatus != '')
            {
                $d = strtotime($row[1]) - strtotime($storedTime);
    
    //            printf ('Old: %d  New: %d   Diff: %d  <br/>', $prevStatus, $row[2], $d);
            }
    array_push($records,"$row[0]#$storedTime#$row[1]#$d");
            $prevStatus = $row[2];
            $storedTime = $row[1];
    }
    
    }
    //echo '</pre>';
    
     echo '<TABLE borderColor=#006699 cellSpacing=1 cellPadding=1 overflow:auto; overflow-x:hidden; width="100%" border=1>
        <TBODY>
        <TR>
          <TD align=middle bgcolor=#006699 width=79><font size=2 color=#ffffff><b>Name</b></font></TD>
          <TD align=middle bgcolor=#006699 width=92><font size=2 color=#ffffff><b>Start Time</b></font></TD>
          <TD align=middle  bgcolor=#006699 width=102><font size=2 color=#ffffff><b>End Time</b></font></TD>
          <TD align=middle  bgcolor=#006699 width=102><font size=2 color=#ffffff><b>Time difference<b></font></TD>
        </TR>';
    foreach($records as $rec){
    $data=explode("#",$rec);
    
                 echo "<tr>";
                echo "<td width=60 align=center bgcolor=$color><font size=2>$data[0]</font></td>";
                echo "<td width=60 align=center bgcolor=$color><font size=2>$data[1]</font></td>";
                echo "<td width=100 align=center bgcolor=$color><font size=2>$data[2]";
                echo "<td width=140 align=center bgcolor=$color><font size=2>$data[3]</font></td></tr>";
    }
    
    echo '</table>'; 
    
    

      ehm.. Dificult to help with so little to go on.. but ehm.. assuming you have the timestamps & status in a simple array:

      $timestampset= array('status'=>$value, 'timestamp'=>$timevalue);

      $loop = 0;
      foreach($timestampset as $key => $TSS)
        {
        if($loop == 0)
          {
          $lasttime = $TSS['timestamp'];
          $LastStatus = $TSS['status'];
          $loop = 1;
          }
        if($LastStatus <> $TSS['status'])
          {
          $timediff = $TSS['timestamp'] - $lasttime;
          }
        else
          {
          $timediff = '';
          }
        ; setup for next loop
          $lasttime = $TSS['timestamp'];
          $LastStatus = $TSS['status'];
      
        echo $LastStatus."|".$TSS['timestamp']."|".$timediff;
        }
      

      Obviously you need to play with the formatting of times and stuff.

        Ehm.. I am not sure but what is the difference between this thread and the other one to which I just wrote a reply?

        Please do not clutter the forums with duplicate posts

                  $recordsdata = reportdowntime($start_dt);
          
          
                foreach($recordsdata as $rec){
                              $val[$count] = explode("#",$rec);
                              $count++;
                      }
          
          
          for($i=0;$i<$count;$i++){
          //$val[$i][2]              =  preg_replace("/_/"," ",$val[$i][2]);
          $record_array[]=array('status'=>$val[$i][0], 'timestamp'=>$val[$i][1],'node'=>$val[$i][2]);
          
          }
          $loop=0;
          
          foreach($record_array as $key => $TSS){
          if($loop==0){
          $lasttime=$TSS['timestamp'];
          $lastStatus=$TSS['status'];
          $loop=1;
          }
          if($lastStatus <> $TSS['status']){
                  $timediff=$TSS['timestamp']-$lasttime;
                          }
          
              else {
              $timediff = '';
              }
          echo $lastStatus."|".$TSS['timestamp']."|".$timediff."|".$TSS['node'];
          }
          
          

          Here I get the difference for each timestamp.

          Status is 0 for all the name then only first and last row I have to push into array.I should calcu;ate the difference only when status is changed from 0 to 1 and not from 1 to 0. So please tell me how to do it

            well..

            First of all: You did not use all of the post; you do not initialize the next loop. Once you do that, you will see that it echos the time difference for each change in status. From there it is an easy step to change that to showing each change in status, when the new status is 0. This however is not what yu asked initially, your example shows that it should calculate the diff whenever the status changes. I'll leave the last step for you to work out, as a learning experience 😉

              Write a Reply...