Elapsed Time to Database with Javascript Timer

I have the following code that will count the elapsed time and print the value in seconds after the user presses the stop button. Our first problem is that we are trying to get her to publish the elapsed time in hours. So let's assume that the elapsed time is 20 minutes, the output will be 0.3 hours.

Secondly, we are trying to get this value, placed in a table inside our created MySQL database. Its int value in our table, just don't know how to send it there. Any help is appreciated!

<script type="text/javascript"> // Javascript to compute elapsed time between "Start" and "Finish" button clicks function timestamp_class(this_current_time, this_start_time, this_end_time, this_time_difference) { this.this_current_time = this_current_time; this.this_start_time = this_start_time; this.this_end_time = this_end_time; this.this_time_difference = this_time_difference; this.GetCurrentTime = GetCurrentTime; this.StartTiming = StartTiming; this.EndTiming = EndTiming; } //Get current time from date timestamp function GetCurrentTime() { var my_current_timestamp; my_current_timestamp = new Date(); //stamp current date & time return my_current_timestamp.getTime(); } //Stamp current time as start time and reset display textbox function StartTiming() { this.this_start_time = GetCurrentTime(); //stamp current time document.TimeDisplayForm.TimeDisplayBox.value = 0; //init textbox display to zero } //Stamp current time as stop time, compute elapsed time difference and display in textbox function EndTiming() { this.this_end_time = GetCurrentTime(); //stamp current time this.this_time_difference = (this.this_end_time - this.this_start_time) / 1000; //compute elapsed time document.TimeDisplayForm.TimeDisplayBox.value = this.this_time_difference; //set elapsed time in display box } var time_object = new timestamp_class(0, 0, 0, 0); //create new time object and initialize it //--> </script> <form> <input type="button" value="Start" onClick="time_object.StartTiming()"; name="StartButton"> </form> <form> <input type="button" value="Finish" onClick="time_object.EndTiming()"; name="EndButton"> </form> <form name="TimeDisplayForm"> Elapsed time: <input type="text" name="TimeDisplayBox" size="6"> seconds </form> 
0
source share
1 answer
  • Date.getTime() returns the time in milliseconds, so this_time_difference will have a time difference in milliseconds. All you have to do is divide by 36,00,000 (1 hour = 60 minutes * 60 seconds * 1000 milliseconds) to get the value in hours.

     var timeInHours = this_time_difference/3600000; 
  • I'm not sure if you just POSTing this data (using Ajax or the plain old submit form ) to a PHP script that inserts this value into your DB - more information about where you are stuck will help you get more specific answers.

+1
source

All Articles