Reputation: 345
I am developing a typing application using PHP and MySQL and little bit of javascript. The PHP code is used to get the server time. I am using the following javascript function to display the start time and end time(by adding 5 min with the minute value of start time) in the browser.
function display(){
// get the server time using PHP and print the start time and end time
var servDateArray='<?php print date("Y/n/d/H/i/s", time())?>'.split('/');
starttime_year = servDateArray[0];
starttime_month = servDateArray[1];
starttime_day = servDateArray[2];
starttime_hour = servDateArray[3];
starttime_minutes = servDateArray[4];
starttime_seconds = servDateArray[5];
// print the Start time
document.getElementById("starttime_echoed").innerHTML= starttime_hour+":"+starttime_minutes+":"+starttime_seconds;
document.typing.starttime.value=starttime_hour+":"+starttime_minutes+":"+starttime_seconds;
// print the End time
endtime_hour = parseInt(starttime_hour) + 0;
endtime_min = parseInt(starttime_minutes) + 5;
endtime_sec = parseInt(starttime_seconds) + 0;
if (endtime_sec>=60) { endtime_sec = -(60 - endtime_sec); endtime_min = parseInt(endtime_min)+1; }
if (endtime_min>=60) { endtime_min = -(60 - endtime_min); endtime_hour = parseInt(endtime_hour)+1; }
if (endtime_hour>=24) { endtime_hour = -(24 - endtime_hour); }
if (endtime_sec<=9) { endtime_sec="0"+endtime_sec; }
if (endtime_min<=9) { endtime_min="0"+endtime_min; }
if (endtime_hour<=9) { endtime_hour="0"+endtime_hour; }
document.getElementById("endtime_echoed").innerHTML= endtime_hour+":"+endtime_min+":"+endtime_sec;
document.typing.endtime.value=endtime_hour+":"+endtime_min+":"+endtime_sec;
displayTimer();
}
The above code works perfectly well for all input values of Hours, minutes and seconds excepts when hour, minute and second are set to either 8 or 9. However, I have tried to debug by inserting a line of code alert(parseInt(starttime_minutes)); after the line endtime_min = parseInt(starttime_minutes) + 5; and it returned 0 when I supplied 8 or 9 as value for minute. I am sure this function returns 0 for hours, minutes and seconds. To be very specific I am giving you am example: If the server time is say 08:09:37 then the end time becomes 00:05:37. I am getting perplexed. Please help.
Upvotes: 2
Views: 75
Reputation: 360772
Ignacio's got the right answer, but I'm going to follow up with some other suggestions. Your code is a horrible mess, doing all kinds of unecessary date manipulations in a very overcomplicated fashion. You could reduce a large chunk of the script to this:
<?php
$now = time();
?>
var startTime = new Date(<?php echo $now * 1000 ?>);
var endTime = new Date(<?php echo ($now + (5 * 60)) * 1000?>);
The JS date object will give you the current hours/minutes/seconds with startTime.getMinutes()
, .getHours()
, .getSeconds()
, etc... There's no need to extract it into separate variables.
For the time update calculations, you simply do things like:
var newTime = new Date(startTime.getTime() + 1000); // increment by 1 second
rather than your ugly "add a second, check if it overflowed, then update minutes, if minutes overflow, update hours, etc..." code. JS can do that all for you with this one single line of code.
Upvotes: 1
Reputation: 799160
08
and 09
are not valid numbers, since the 0
prefix indicates that the number is octal, and octal digits only go up to 7
. Drop the initial 0
.
Upvotes: 4