Jump to content


Photo

Pesky timestamps


  • Please log in to reply
4 replies to this topic

#1 Traces

Traces
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 October 2006 - 05:31 AM

I am trying to compare two timestamps in a log file to calculate the time between them, however the timestamps in the log file are in military time and don't include actual dates or anything else.  The problem is that if the log file isn't written to every single hour (out of my control when it will be) or the system is shut off for a while and turned back on I can't compare them if it falls between 2300 hours and 0100 hours for example or 2300 hours one day and 2300 hours the next.  The php file will be checking them at random as well.

[23:12:01] Entry
[01:20:56] Entry

Is there even a solution to this?  It seems unix timestamps is the solution in most of the search results I saw but I don't see how it will work for me here.

#2 Hi I Am Timbo

Hi I Am Timbo
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 03 October 2006 - 05:50 AM

Say you get the hours, minutes and seconds from the string. 

You can compare them by converting them to seconds.

<?php
$seconds1 = $hours1 * 3600 + $minutes1 * 60 + $seconds1;
$seconds2 = $hours2 * 3600 + $minutes2 * 60 + $seconds2;
?>


#3 Traces

Traces
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 October 2006 - 06:30 AM

The only problem is it still doesn't tell me whether or not the log file was last updated a day or even a week ago for example.  Also, if I am reading that right that would still give me issues as 2300 hours turned into seconds gives me a higher number than 1000 hours still.

Thinking about it some more though I just realized that I could tell it that if the first number (23:12:01) in my example is larger than the second entry (01:20:56) to simply count backwards...  I am going to test this theory and see where that gets me.

I still don't see how I am going to get over the issue of it "finding its bearings" as it will never (seemingly) be able to tell the difference between 2300 hours of last year to today without something extra to go off of.

Thanks Timbo.

#4 Hi I Am Timbo

Hi I Am Timbo
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 03 October 2006 - 07:17 AM

It is a good thing that 2300 hours turned into seconds is higher than 1000 hours turned into seconds as that is the case in the hours format as well. 

Simply subtract as follows:  23 * 3600 - 10 * 3600 = number of seconds difference.

If you don't know what day these timestamps occurred on, there is no real way to find out unless they happen at least once a day, wich may or may not be the case. 

But if they do, then detecting if one is less than the other would work.

#5 Fehnris

Fehnris
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 04 October 2006 - 01:44 AM

I can't see a solution to you being able to compare the two entries because as you say all you have to work with is times and no dates. 

If the file is writen to sequentially then chances are the second entry time is closer to the present than the first entry time. 

Thats about all the information you can get from it.  No way of telling, that I can think of, if the second time is the same day, the next day or the next week.  Only indication of the second time being not the same day would be if the second time is earlier than the first.

I had an idea of maybe checking the log file with filemtime() to get the last time the file was edited, but all that will tell you is the date/time the last entry was added to the log file.  That would still leave you with the problem of figuring out a date for the first entry to compare the two dates.

What level of access do you have to the log file creation?  i.e do you have a say in what format to save the date/time to the log file?  If so I would suggest the date/time, for future entries to include date and time in some format.  Sorry I can't be of more help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users