1

I'm trying to figure out a way to create a function to have the user be able to start a project by clicking a button and the timer would start. At the same time, I would like the user to be able to pause as well as stop the timer. When finished with a task, they can then hit 'Finished' to record the total amount of recorded time, minus any paused time, for the task to a variable in which I can record.

I have been researching and found that a Javascript timer wouldn't be very reliable which is why I wanted to go with Php. I figured I could take a Timestamp on 'Start' and 'Stop', but since I'm needing a 'Pause' as well, it's throwing a whole new kink into my plans.

Anyone know of a good way to accomplish this? I know that one Timestamp to another will get the total time, but how can you calculate one to another if the process had been paused?

4
  • 2
    The precision certainly is absolutely not different between the two languages. Question is what is better: to keep the timer on the client side or on the server side. The client side is much easier to implement, but is gone as soon as the browser window is closed or similar. The server side timer requires some sort of session management and it must be controlled from the client side by means of forms or javascript, so it is much more complex. This all obviously assumes that you are talking about some web based solution, which is not clear from your question. Commented Aug 22, 2015 at 17:02
  • as @arkascha says, this seems to geared for a client side script, and if you wouldn't mind quoting this 'research' you have saying JS timers aren't accurate, as well. Commented Aug 22, 2015 at 17:07
  • @Quill I could imagine the source meant that a JS based times is not accurate in the sense of reliable from a security point of view since it can be manipulated more easily. Commented Aug 22, 2015 at 17:10
  • I did not mean not accurate. I meant reliable, and changed my description as well. This is a web based application, so it will have session management and will be controlled by a Javascript Event Handler. Commented Aug 22, 2015 at 17:32

1 Answer 1

2

I don't think "Javascript timer wouldn't be very accurate" is an accurate statement.

However, if you decide to use PHP, you will not be having the script running for the whole duration of the project. What you need is simply a number of markers in an object. You can store the object in a database table or as JSON string in a file. Here's an example using JSON:

[
  [1440264185, "start"],
  [1440280217, "pause"],
  [1440288349, "resume"],
  [1440292161, "pause"],
  [1440317465, "resume"],
  [1440325597, "stop"]
]

The object is built progressively with each event adding the timestamp of the signal received with the signal type (start, pause, resume, stop).

Then, when you want to calculate the amount of time spent working on the project/task you can use code like this:

$markers = json_decode($json_markers, true);
$num_markers = count($markers);
$markers[] = $markers[$num_markers-1];
$time_worked = 0;
for($i = 0; $i < $num_markers; $i++) {
  if(in_array($markers[$i][1], array("start","resume"))) {
    $time_worked += $markers[$i+1][0] - $markers[$i][0];
  }
}
var_dump($time_worked); // int(27976)

Here's the code above.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a solution that will work perfectly for me and will definitely get the ball rolling in the right direction! Many thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.