我有以下代码,它会计算经过的时间,并在用户点击停止按钮后以秒为单位打印该值。我们的第一个问题是我们试图让它以小时为单位发布经过的时间。因此,假设经过的时间是20分钟,输出将是.3小时。
其次,我们试图将此值发布到我们创建的MySQL数据库中的表中。它是我们表中的一个int值,只是不知道如何在那里发布它。任何帮助表示赞赏!
<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 :(得分:1)
Date.getTime()
以毫秒为单位返回时间,因此this_time_difference
将具有以毫秒为单位的时间差。您需要做的就是除以36,00,000
(1小时= 60分钟* 60秒* 1000毫秒)以获得以小时为单位的值。
var timeInHours = this_time_difference/3600000;
我不确定您将此数据(使用Ajax或plain old form submit)发布到将数据值插入数据库的PHP脚本时会出现什么问题 - 更多信息你被困的地方会帮助你得到更具体的答案。