rashmi_k28 Posted June 11, 2008 Share Posted June 11, 2008 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>'; Link to comment https://forums.phpfreaks.com/topic/109704-time-difference/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.