I wrote a simple function to calculate the difference between two times within a 24 hour period.
Code:
function calculateTime(time1,time2) {
var hours1 = parseInt(time1.slice(0,2),10);
var mins1 = parseInt(time1.slice(3,5),10);
var secs1 = parseInt(time1.slice(6,8),10);
var hours2 = parseInt(time2.slice(0,2),10);
var mins2 = parseInt(time2.slice(3,5),10);
var secs2 = parseInt(time2.slice(6,8),10);
if (hours1>hours2) {
hours2=hours2+24;
}
var startTime = (hours1 * 3600) + (mins1 * 60) + (secs1);
var endTime = (hours2 * 3600) + (mins2 * 60) + (secs2);
var totalSecs = endTime-startTime;
var sumHours = parseInt(totalSecs / 3600, 10);
var sumMins = parseInt((totalSecs - sumHours * 3600) / 60, 10);
var sumSecs = totalSecs - sumHours * 3600 - sumMins * 60;
var i;
var sumTime = Array();
sumTime[0] = sumHours;
sumTime[1] = sumMins;
sumTime[2] = sumSecs;
for (i=0;i<sumTime.length;i++) {
if(sumTime[i] < 10) {
sumTime[i] = "0" + sumTime[i];
}
}
return sumTime.join(':');
}
This works great but I can't figure out how to call the function. I want to create a table where users will enter their start and end times into text boxes in a <tr> and have the result displayed in a cell on the same row.
Any ideas? This one seems simple but I seem to be at a loss. I thought about using window.onLoad but not sure that's the best (if even possible) approach.
the problem with text boxes is that they have an onchange event, but it is called when the text box loses focus, which is kind of counter-intuitive to some users. I would suggest having a "calculate time" button in the same row that the users clicks once both boxes are filled out. From there it is a simple matter of finding the two textboxes in the DOM, grabbing their values and feeding them to your function.
function getTimes(btn){
var start=btn.parentNode.parentNode.cells[0].children[0].value;
var end=btn.parentNode.parentNode.cells[1].children[0].value;
btn.parentNode.parentNode.cells[3].innerHTML=calculateTime(start,end);
}
Last edited by xelawho; 09-20-2012 at 05:50 PM..
Reason: added example
Thanks for your reply. Unfortunately I don't have the luxury of a button. Actually I was just informed that I have to set up a "confirmation" page that will display the start and end times entered from a form on another page, so this just became a little more complicated.
I can echo the entered values to my table cells using PHP but I am still unsure how I will get the result:
Code:
<table>
<tr>
<th>Start time:</th>
<th>End time:</th>
<th>Total time:</th>
</tr>
<tr>
<td><?php echo $row_rstAffectedProg['StartTime']; ?></td>
<td><?php echo $row_rstAffectedProg['EndTime']; ?></td>
<td> NOT SURE WHAT TO DO HERE :( </TD>
</tr>
</table>
hmm. I think the last event you can listen for is the window.onload, and if you are lucky this happens after the php has been echoed in. I really don't know about the order of things there.
If it is true and you can give your table an id of "tab" you could try this:
Code:
window.onload=function (){
var rows=document.getElementById("tab").getElementsByTagName("tr");
for (var x = 1; x < rows.length; x++) { //start at 1 to skip the header cells
var start=rows[x].cells[0].children[0].value;
var end=rows[x].cells[1].children[0].value;
rows[x].cells[2].innerHTML=calculateTime(start,end);
}
}
</script>
but are you sure that the start and end times will be displayed in text boxes? because if they just get displayed as text you would have to do:
Code:
var start=rows[x].cells[0].innerHTML;
var end=rows[x].cells[1].innerHTML;
but (again flaunting my ignorance of php) if these figures are coming from the server, can't you do the calculations server side and just echo in all the results?
but (again flaunting my ignorance of php) if these figures are coming from the server, can't you do the calculations server side and just echo in all the results?
I am more comfortable with javascript and I like the idea that the calculations will be performed client side. BTW your function worked nicely. Big thanks :-)