[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

Convert log times to difference, PHP

Can someone see a quick and dirty way to convert the following curl log from:

10:56:46.344794 <= Recv header, 17 bytes (0x11)
10:56:46.344855 <= Recv header, 36 bytes (0x24)
10:56:46.344889 <= Recv header, 25 bytes (0x19)

to:
0.000000 0.000000 <= Recv header, 17 bytes (0x11)
0.000061 0.000061 <= Recv header, 36 bytes (0x24)
0.000034 0.000095 <= Recv header, 25 bytes (0x19)

Basically showing both the difference in time and running difference total from the previous line (line 1 being zero time)

Would be very grateful if someone has the time to work this out?

Thanks
BT


0
brothertom
Asked:
brothertom
  • 2
1 Solution
 
Beverley PortlockCommented:
This code

<?php

$data = array(
               '10:56:46.344794 <= Recv header, 17 bytes (0x11)',
               '10:56:46.344855 <= Recv header, 36 bytes (0x24)',
               '10:56:46.344889 <= Recv header, 25 bytes (0x19)'
             );


$first = true;
$startTime = 0;
$total     = 0;

foreach ( $data as $anItem ) {

     if ( preg_match( '#^(\d+):(\d+):([\.0-9]+)\s(.*?)$#', $anItem, $matches ) ) {

          $time = $matches[1] * 3600 + $matches[2] * 60 + $matches[3];

          if ( $first ) {
               $first = false;
               $startTime = $time;
          }
          
          $diff = $time - $startTime;
          $total += $diff;

          printf("%07.6f %07.6f %s", $diff, $total, $matches[4] );
          echo "<br/>";

          $startTime = $time;

     }
     
}

Open in new window


Produces this

0.000000 0.000000 <= Recv header, 17 bytes (0x11)
0.000061 0.000061 <= Recv header, 36 bytes (0x24)
0.000034 0.000095 <= Recv header, 25 bytes (0x19)

0
 
brothertomAuthor Commented:
Perfect - thank you very much  :)
0
 
brothertomAuthor Commented:
Top solution :)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now