Convert log times to difference, PHP

Posted on 2011-10-18
Last Modified: 2012-05-12
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)

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?


Question by:brothertom
    LVL 34

    Accepted Solution

    This code

    $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)


    Author Comment

    Perfect - thank you very much  :)

    Author Closing Comment

    Top solution :)

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now