I am attempting to write a JS count up timer (that will just count up to infinity) based on a PHP variable. The variable unfortunately is uneditable (can do things to it once I get it, but can't change the form of what I get since it's coming from a database software writes into, and I can't change the software). All I get is seconds (how many seconds ago something user accesed, etc) and would like that to be displayed in a format of (X Days, X hours, X minutes, X seconds).
I already have a code for the timer and the PHP code too, however the question is what would be the best approach at the solution. Do I try to get the seconds into a date in PHP then let JS do all rest or do I change JS so that it accepts the seconds and then do the countdown. As well as this, how would I do it?
Code I have for the counter:
JS inside the document, passing in the variables so that PHP can change these easily:
<script type="text/javascript" language="JavaScript">
TargetDate = "09/21/2012 5:00";
CountActive = true;
</script>
and then (including all the counter code):
<script type="text/javascript" language="JavaScript" src="js/time.js"></script>
And the .js file as mentioned above:
function calcage(secs, num1, num2) {
s = ((Math.floor(secs/num1))%num2).toString();
if (LeadingZero && s.length < 2)
s = "0" + s;
return s;
}
function CountBack(secs) {
if (secs < 0) {
document.getElementById("cntdwn").innerHTML = FinishMessage;
return;
}
DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));
document.getElementById("cntdwn").innerHTML = DisplayStr;
if (CountActive)
setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
}
function putspan(backcolor, forecolor) {
document.write("<span id='cntdwn'></span>");
}
if (typeof(BackColor)=="undefined")
BackColor = "white";
if (typeof(ForeColor)=="undefined")
ForeColor= "#2A8827";
if (typeof(TargetDate)=="undefined")
TargetDate = "12/31/2020 5:00 AM";
if (typeof(DisplayFormat)=="undefined")
DisplayFormat = "%%D%% days, %%H%% hours, %%M%% minutes, %%S%% seconds.";
if (typeof(CountActive)=="undefined")
CountActive = true;
if (typeof(FinishMessage)=="undefined")
FinishMessage = "no data";
if (typeof(CountStepper)!="number")
CountStepper = +1;
if (typeof(LeadingZero)=="undefined")
LeadingZero = true;
CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 1000;
putspan(BackColor, ForeColor);
var dthen = new Date(TargetDate);
var dnow = new Date();
if(CountStepper>0)
ddiff = new Date(dnow-dthen);
else
ddiff = new Date(dthen-dnow);
gsecs = Math.floor(ddiff.valueOf()/1000);
CountBack(gsecs);
As advised in the comments, I used PHP. Got to it eventually, by:
Getting the value from DB and storing it within $uptime (in the form of seconds).
$uptime = (time() - $uptime);
$uptime = date("m/d/Y H:i:s",$uptime);
Then I changed it using the date function to suit the JS code and output it with the new $uptime variable:
<script type="text/javascript" language="JavaScript">
TargetDate = "<?php echo $uptime; ?>";
CountActive = true;
</script>
<script type="text/javascript" language="JavaScript" src="js/time.js"></script>
This made sure the date is outputted in the correct format, suitable for JS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With